= 5.4, you may encounter the error "Creating default object..."/> = 5.4, you may encounter the error "Creating default object...">
Home >Backend Development >PHP Tutorial >How to Solve the 'Creating default object from empty value' Error in PHP?
Handling "Creating Default Object from Empty Value" Error in PHP
In PHP versions >= 5.4, you may encounter the error "Creating default object from empty value" when attempting to access a property on an uninitialized object. This error signifies that the referenced variable is either null or not an object.
Recommended Solution:
To resolve this issue, declare the variable as an instance of the stdClass object in the global namespace before accessing its properties:
$res = new \stdClass(); $res->success = false;
Explanation:
With E_STRICT warnings enabled in PHP <= 5.3.x or error_reporting set to E_WARNING in PHP >= 5.4, PHP strictly enforces object manipulation. Attempting to access properties on a null or non-object variable will trigger an error. Declaring the variable as a stdClass object ensures that it is an object instance and can have properties assigned to it.
Alternative Approaches:
Alternatively, you can use the following approach to avoid this error:
if (!isset($res) || !is_object($res)) { $res = new \stdClass(); }
$res = (object) ['success' => false];
The above is the detailed content of How to Solve the 'Creating default object from empty value' Error in PHP?. For more information, please follow other related articles on the PHP Chinese website!