基于PHP

时间:2016-02-16 02:07:50

标签: php oop

我有一个稍微不同的问题,我在另一个类中实例化一个类,当我手动调用该方法但是在我自动化它时没有。代码如下:

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没有给出任何错误/警告。它只是沉默而忘记了按钮。有谁看到这里出了什么问题?谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

现在无法发表评论......只有一件事:json_decode()会失败。要接收有效的json更改{filler:true,fill:sometext}{"filler": true, "fill": "sometext"}