You may encounter this “warning” message when using PHP 5.4 or above
E_WARNING: Creating default object from empty value
Here is how to fix this 🙂
Sample code that generates error :
<?php
$a->b = 'something';
?>
How to fix :
<?php
if (is_null($a) || !is_object($a)) {
$a = new stdClass();
}
$a->b = 'something';
?>
Sample code that generates error :
<?php
$a->b->c = 'something';
?>
How to fix :
<?php
if (is_null($a) || !is_object($a)) {
$a = new stdClass();
}
if (is_null($a->b) || !is_object($a->b)) {
$a->b = new stdClass();
}
$a->b->c = 'a';
?>
One reply on “How to Fix WARNING: Creating default object from empty value”
Hello,
Thank you very much. I appreciate a lot. This cause me big headache for a whole day and finally, I got the solution here.