从空值创建默认对象 php 5,5

Creating default object from empty value php 5,5

我尝试创建新的 stdClass 但仍然没有,此代码出错: 警告:从空值创建默认对象。

function __construct($params) {
    parent::__construct($params);
    //$this -> view -> controller = null;
    $this -> view -> controller = new stdClass(); // <-- HERE IS ERROR 
    $this -> view -> controller = 'Index';

    require_once 'models/_index_model.php';
    $this -> model = new Index_model();

    $action = 'News';
    if(isset($params[1])){ $action = ucfirst($params[1]); }

    $this->date = 'Today';
    if(isset($params[2])) $this->date = ucfirst($params[2]);

    $this->$action($this->date);
}

问题是,变量 $view

没有值

我不确定你想要实现什么,但如果你像下面的例子那样实现它,代码应该可以工作:

function __construct($params) {
    parent::__construct($params);

    $this -> view = new stdClass(); // create a new stdClass object
    $this -> view -> controller = 'Index'; // assign the controller value

    // ... and so on
}

所以这里的主要问题是变量$view没有声明,或者它的值不是一个对象。

如有更多问题欢迎在评论区提问