Skip to content
Advertisement

PHP 7.4 Warning: Creating default object from empty value

The apache error logs are filling up with this. I don’t want to suppress all errors though, and understand I need to create an object explicitly somewhere but the syntax escapes me.

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

class Helper {
    use G2LTGetSet;

    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;
            }
        }
    }

}

Advertisement

Answer

The problem is that assuming view is null, you should not refer its items. You can do it like this:

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;
        }
    }
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement