0
我只是試驗OOP編程我正在嘗試創建一個表單類。我無法打印複選框,我如何查看事情發生的地方?基本OOP PHP表單類
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;
}
}
那麼啓用你的error_reporting和display_errors ini設置呢? – bwoebi
就像一條建議,我認爲你應該將'show_checkbox()'方法重命名爲像'render()'這樣更通用的方法,並且還要爲InputField類編寫'render()'方法。這將使您能夠根據需要在任意多個類中擴展InputField,並循環遍歷從InputField繼承的許多對象,並調用它們的render()方法,而無需知道它們的真實含義。看看[多態](http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29)瞭解更多細節。 –