PHP 7.4 警告:从空值创建默认对象

PHP 7.4 Warning: Creating default object from empty value

apache 错误日志中充满了这个。不过,我不想抑制所有错误,并且我明白我需要在某处显式创建一个对象,但语法让我不知所措。

Warning: Creating default object from empty value in libraries/cegcore2/libs/helper.php on line 22

class Helper {
    use \G2\L\T\GetSet;

    var $view = null;
    var $_vars = array();
    var $data = array();
    var $params = array();

    function __construct(&$view = null, $config = []){
        $this->view = &$view;
        $this->_vars = &$view->_vars; // <---- Line 22
        $this->data = &$view->data;

        if(!empty($config)){
            foreach($config as $k => $v){
                $this->$k = $v;
            }
        }
    }

}

问题是假设视图是 null,您不应该引用它的项目。你可以这样做:

function __construct(&$view = null, $config = []){
    $this->view = &$view;
    if ($view) {
        $this->_vars = $view->_vars; // <---- Line 22
        $this->data = $view->data;
    }

    if(!empty($config)){
        foreach($config as $k => $v){
            $this->$k = $v;
        }
    }
}