我有一個稍微不同的問題,我在另一個類中實例化一個類,當我手動調用該方法時,該類不起作用,但不會自動化。代碼如下:實例化一個基於PHP中的參數的類
class Button {
function __construct($params = Array()) {
// get some common properties
}
}
class HelperButton extends Button {
function __construct($params = Array()) {
parent::__construct($params);
}
function getHelperButton() {
//generate helper button code
}
}
我使用上面的類以下類
include_once('field.class.php');
include_once('buttons.class.php');
class Field {
function __construct() {
}
function setName($name){
$this->name = $name;
}
}
class TextField extends Field {
function __construct() {
parent::__construct();
}
function setFiller($fill = "") {
$helperbtn = new HelperButton($fill);
$this->helperbtn = $helperbtn->getHelperButton();
}
function getTextField(){
$this->textfield = "<input name='blah' />".$this->heperbtn;
return $this->textfield;
}
}
class Segment extends Field {
function __construct() {
parent::__construct();
}
function addTextField($params = array()) {
if(is_array($params)){
$txtfld = new TextField();
}
if (isset($params['type']['filler'])) {
$txtfld->setFiller($params['type']['fill']);
}
$this->segment .= $txtfld->getTextField();
}
function addFillerField($params = array()) {
$params['type']['filler'] = true;
$this->addTextField($params);
}
}
現在,我使用參數生成文本字段在我的頁面動態。以下代碼運行良好:
$segment = new Segment();
$segment->addFillerField("type"=>array("filler"=>true,"fill"=>"sometext")); //this should add a button next to the field which will open a window that will load the 'fill' text
$segment->render(); // this will print the segment output to screen
但是,下面的代碼不起作用。它打印文本字段好,但它不打印按鈕:
$fldmthds = array("FLRFLD" => "FillerField");
$pagedtl = array("FCLSID" => "FLRFLD", "FLDNAM" => "Field Name", "FLDTYP" => "filler:true,fill:sometext");
$mysegment = new Segment();
foreach ($pagedtl as $flds => $val) {
$mthd = "add" . $fldmthds[$val['FCLSID']]; // resolves to addFillerField
$params = array(
'name' => $val['FLDNAM'],
'type' => json_decode("{" . $val['FLDTYP'] . "}", true)
);
$mysegment->{$mthd}($params);
}
$mysegment->render();
PHP沒有給出任何錯誤/警告。它只是沉默,忘記了按鈕。有人看到這裏出了什麼問題嗎?謝謝你的時間。
'$ fldmthds = array(「FLRFLD」=>「FillerField」);'在行結尾缺少結尾分號(;),您知道嗎? –
謝謝湯姆。當我複製粘貼時,這是一個錯過。 – Ravi
謝謝。您還可以顯示您的「Field」類的代碼嗎? –