How to Fix 'Creating Default Object from Empty Value' Warning in PHP

Creating default object from empty value in PHP?

Your new environment may have E_STRICT warnings enabled in error_reporting for PHP versions <= 5.3.x, or simply have error_reporting set to at least E_WARNING with PHP versions >= 5.4. That error is triggered when $res is NULL or not yet initialized:

$res = NULL;
$res->success = false; // Warning: Creating default object from empty value

PHP will report a different error message if $res is already initialized to some value but is not an object:

$res = 33;
$res->success = false; // Warning: Attempt to assign property of non-object

In order to comply with E_STRICT standards prior to PHP 5.4, or the normal E_WARNING error level in PHP >= 5.4, assuming you are trying to create a generic object and assign the property success, you need to declare $res as an object of stdClass in the global namespace:

$res = new \stdClass();
$res->success = false;

php Object Class Error: Creating default object from empty value

does this help you or am I misunderstanding...

public function __construct($user, $token) {
$this->status = new stdClass(); // Here
$this->status->version=2.3;
$this->authentication = new stdClass(); // & Here
$this->authentication->user=$user;
$this->authentication->token=$token;
}

Warning: Creating default object from empty value (typical solution not solving issue)

If you want to avoid the warning you'll need to pre-create each level:

$data = new stdClass();
$data->result = new stdClass();
$data->result->complex = new stdClass();
$data->result->complex->first_key = $old_data->result->complex;

PHP Array Warning: Creating default object from empty value

You get the warning

Warning: Creating default object from empty value

because you do

$searcharticles->$postid->post_id = $postid;

Your $searcharticles is a StdClass. You assign the property $postId and then immediately chain off another property post_id, which forces PHP to create a new default object.

To avoid it, put this before the chained method calls:

$searcharticles->$postid = new StdClass;
$searcharticles->$postid->post_id =$postid;
$searcharticles->$postid->title =$title;
$searcharticles->$postid->titlescore = $titlescore;
$searcharticles->$postid->quality =$quality;

How to fix 'Creating default object from empty value' warning in PHP

As it turns out, the author missed a very simple fix and general good practice that you should always initialize your object before you try to set a property. The very simple fix for this is simply to add a new StdClass; call right before the error with the variable it is trying to access.

$items[$i] = new StdClass;
$items[$i]->title = $crs_post_title;

That first line will fix the warning from showing up.

This would also fix the problem in /components/com_community/models/activities.php on line 387 with the following fix.

$commentsResult[$comment->type . '-' . $comment->contentid] = new StdClass;
$commentsResult[$comment->type . '-' . $comment->contentid]->_comment_count = 0;

Warning: Creating default object from empty value Line 63

You have probably never initialize $shop.

Just add at the top:

$shop = new stdClass();

PHP 7.4 Warning: Creating default object from empty value

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


Related Topics



Leave a reply



Submit