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
When and Where Should I Use Session_Start
Getting the Text Portion of a Node Using PHP Simple Xml
Opposite of Nl2Br? Is It Str_Replace
Data Transfer from JavaScript to PHP
How to Deploy Correctly When Using Composer's Develop/Production Switch
Gmail Like File Upload with Jquery
How to Perform Multiple Guzzle Requests at the Same Time
Reverse Order of Foreach List Items
Utf-8 Not Working in HTML Forms
Php, Merging Arrays with Common Keys
Mysqli_Fetch_Array Returning Only One Result
Check If Value Exists Before Inserting into MySQL Db in a PHP Script
How to Use PHP to Get the Current Year
How to Make Short Random Unique Keys, Like Youtube Video Ids, in PHP
How to Check If an Integer Is Within a Range of Numbers in PHP