0

フォーム クラスを作成しようとしている OOP プログラミングを試しています。チェックボックスを印刷できません。どこで問題が発生しているのかを確認するにはどうすればよいですか?

  require_once('form.php');
  $gender = new Checkbox('Please select your gender','gender',array('male','female'));
  echo $gender->show_checkbox();

クラスを含むファイル:

class Inputfield{

  public $id;
  public $options_array;
  public $question;
  public $type;
  function __construct($newquestion,$newid,$newoptions_array){
    $this->question=$newquestion;
    $this->id=$newid;
    $this->type="txt";
    $this->options_array=$newoptions_array;
  }
  public function show_options($options_arr,$type,$id,$classes){
    $output="";
    foreach($options_arr as $option){
        $output.="<label for=\"".$option."\"></label><input name=\"".$option."\" type=\"".$type."\" id=\"".$id."\" class=\"".$classes."\">";
    }
    return $output;
  }
  public function show_question(){
    $output="<h3 class='question'>".$this->vraag."</h3>";
    return $output;
  }
}
class Checkbox extends Inputfield{
  function __construct($newquestion,$newid,$newoptions_array){
    $this->question=$newquestion;
    $this->id=$newid;
    $this->type="checkbox";
    $this->options_array=$newoptions_array;
  }

  public function show_checkbox(){
    $output=show_question();
    $output.=show_options($this->options_array,'checkbox',$this->id,'class');
    return $output;
  }
}
4

2 に答える 2

5
  1. インスタンス メソッドを$this次のように呼び出します。$this->show_options();
  2. 親クラスのコンストラクターと同じになるとすぐに、コンストラクター全体をコピーして貼り付ける必要はありません
    1. 部分的に一致する場合は、次のようparent::__construct(...)に呼び出してカスタムを定義できます$this->type="checkbox";
    2. 実行時に定義することはできませんが、プロパティのデフォルト値として指定できます。
于 2013-07-27T12:06:56.050 に答える
2

$thisオブジェクト コンテキストで使用する必要があります。例えば。show_checkboxメソッドで、次のように記述します。

$output = $this->show_question();
$output .= $this->show_options(...);
于 2013-07-27T12:07:33.893 に答える