html
データ ソースに応じて、表示するために作成する関数が 2 つあります。1 は からdatabase
、もう 1 つは からform
post
です。
function buildForm_newUser($user){
$html_username = 'Username = ' . $_POST['username'];
$html_location = 'location = ' . $_POST['location'];
: // similar rows follow more than 20 times
}
function buildForm_exsitingUser($user){
$html_username = 'Username = ' . $user->get_property('username');
$html_location = 'location = ' . $user->get_property('location');
:
}
たった1つの機能でこれらを達成することは可能ですか?
$user
ソース オブジェクト (すなわち以上)を切り替えようとしまし$_POST
たが、後者の関数は指定されたオブジェクトの関数を使用するのに対し、前者は使用しないためスタックしました。また、いずれかのオブジェクトにアクセスする必要がある行が多数あるため、新しく生成された変数 (上記の例では $html_* 変数) を1 つの場所でのみ宣言するようにしています。とは言っても、私は次のようなものが欲しいです:
function buildForm($user){ // Number and type of argument can vary
// Do something here to specify object type
$html_username = 'Username = ' . // following here, either $_POST or $user->get_property to get 'username'
$html_location = 'location = ' . // following here, either $_POST or $user->get_property to get 'location'
:
}
ありがとう。
(また、より良いタイトルの提案をいただければ幸いです...)