Unfortunately, if you get stuck in the error “Warning: creating default object from empty value” and don’t find out any method for this error. So, you don’t miss this blog. In today’s tutorial, we will give you simple ways to eliminate this problem. Now, let’s get started.

Warning: creating default object from empty value: What causes this error?

Usually, this error will come up, when you are trying to assign properties of an object without initializing an object.

In addition, this error will depend on the configuration in the PHP.ini file. Your new environment may have E_STRICT warning enabled in error reporting for PHP versions <=5.3.x or have error_reporting set to at least E_WARNING with PHP version >=5.4. This error is thrown once $res is NULL or not yet initialized.

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

So, what should you do to handle this problem? Below, we will provide you with 2 methods to help you easily get rid of this error.

How to resolve the error “Warning: creating default object from empty value”

Method 1:Create a stdClass() Object in PHP

This method allows you to declare a variable as an object of stdClass() in the global namespace, you can dynamically assign properties to the objects.

For example, if you generate a variable $obj and set it to NULL, then you set the success property to false with the $obj object. In this situation, the error will appear in the output section since $obj has not been initialized as an object.

Example code:

$obj = NULL;
$obj->success = true;

Then, the output:

Warning: Creating default object from empty value

So, to handle this error, you need to assign the $obj variable with the instance of stdClass(). Then, let’s set the success property to true with $obj.

Next, you need to use the print_r() function to print the $obj object. You can also utilize the isset() function to check if $obj has existed.

When you are done, you will see the information about $obj in the output section.

Example code:

$obj = new stdClass();
$obj->success =true;
print_r($obj);

Then, the output is:

stdClass Object ( [success] => 1 )

Method 2: Create Object from anonymous class in PHP

The second method helps you eliminate the error by creating an object from an anonymous class in PHP and assigning properties to it.

You are able to utilize the new class keyword to generate an anonymous class. Then, let’s set the value of the properties as in the generic class. Because the property will have a class, so you are able to access it with an object. As a result, the error will not appear.

For instance, if you generate an object $obj and assign an anonymous class by using the new class keyword to the object.

Then, you create a public property $success and set the value to true. And outside the class, you need to print the object with the print_r() function. This method will prevent your website from appearing the error by creating an object from an anonymous.

Example code:

$obj = new class {
public $success = true;
};
print_r($obj);

And the output is:

class@anonymous Object ( [success] => 1 )

The bottom lines

Which method is valuable for your error? Let us know your experience and case by leaving a comment below. Moreover, if you are also dealing with any similar trouble, don’t hesitate to ask for our assistance. We will support you as soon as possible.

By the way, it is a great chance for you to drop by our free WordPress Themes to discover a bunch of eye-catching designs. Thank you for your reading!

5/5 - (1 vote)
Lt Digital Team (Content &Amp; Marketing)
Latest posts by LT Digital Team (Content & Marketing) (see all)
Summer Sale Grab 50% Off for everything on today, don't miss it. Coupon code: SUMMER2024 Redeem Now
🎉 Black Friday & Cyber Monday sale! Grab 50% Off for everything on today, don't miss it. Coupon code: BFCM50 Redeem Now