2013-07-27 49 views
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; 
    } 
} 
+2

那麼啓用你的error_reporting和display_errors ini設置呢? – bwoebi

+1

就像一條建議,我認爲你應該將'show_checkbox()'方法重命名爲像'render()'這樣更通用的方法,並且還要爲InputField類編寫'render()'方法。這將使您能夠根據需要在任意多個類中擴展InputField,並循環遍歷從InputField繼承的許多對象,並調用它們的render()方法,而無需知道它們的真實含義。看看[多態](http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29)瞭解更多細節。 –

回答

5
  1. 你叫實例方法與$this$this->show_options();
  2. 你並不需要將整個構造儘快複製粘貼,因爲它是相同的一個在父級
    1. 在情況下,如果它部分匹配,你可以稱它爲parent::__construct(...)然後d自定義$this->type="checkbox";
    2. 您不能在運行時定義它,但將其指定爲屬性默認值。
2

你應該在對象上下文中使用$this。例如。在你的show_checkbox方法中,寫下:

$output = $this->show_question(); 
$output .= $this->show_options(...);