It was recommended I ask this question here as it keeps getting downvoted on Stack overflow.

I am wondering What is the harm in ignoring PHP Warning: Creating default object from empty value?

I don't see any value in "fixing" it if the only thing I am doing is making this warning go away. Does PHP have intentions of making this warning into a fatal error in future versions?

Seems like it can create code bloat, especially in cases where you have dynamically created nested objects. This can be very painful to fix.

** I did search quite a bit to find an answer to this question in both google and stack overflow and feel like this is a good place to have this discussion even if the answers could just be an opinion. I would like to hear what others in the PHP community do to deal with this in their own code.


This will throw two of the aforementioned warnings

if(isset($foo) && isset($name)) {
    $bar->$name->schedule = $foo;

This will throw one warning

$bar = new stdClass();
if(isset($foo) && isset($name)) {
    $bar->$name->schedule = $foo;

This will throw no warnings

$bar = new stdClass();
if(isset($foo) && isset($name)) {
    $bar->$name = new stdClass();
    $bar->$name->schedule = $foo;

This is a basic example, but imagine if we had a huge array of dynamically generated $names that we had to wrap in a foreach loop and now we need to wrap our $bar->$name = new stdClass(); in an if clause to ensure it does not already exist etc. The code starts to get unnecessarily bloated. So bloated I was already annoyed trying to write an example of what I meant. If left as the first one, things just nicely happen with just a few lines.

  • 2
    I know what you're talking about - but could you post a code sample to illustrate your point? – HorusKol Dec 26 '19 at 23:19
  • The value in fixing it somehow is as @RikD says: avoiding the development of tolerance for warnings. – Erik Eidt Dec 27 '19 at 1:19

A warning is just that, a warning that a piece of code is potentially not what you intended.

I’m not familiar with this particular PHP warning and this is also not the place for discussion and opinion as you asked for, but sometimes it’s perfectly fine to ignore warnings. And other times not so much.

It’s up to the professional assessment of the programmer if a warning can or cannot be ignored in specific situations.

Some environments allow you to ignore certain warnings using configurations or tags or other techniques, which is sometimes useful to signal co-workers that a warning can be ignored for a valid reason.

The biggest problem with ignoring (not handling) warnings is that you become “immune” to warnings and you systematically ignore them. Once in a while you will miss an important one and cause a bug which could have been prevented early on in the development process.

So in general I prefer to either fix the warning, or use a technique to remove the warning if it’s invalid for the specific piece of code I’m working on.

  • thanks @Rik D... I definitely do not ignore warnings in general and it hangs in the back of my head that I don't fix this one. It is just that the auto creation of an object from an empty value in PHP used to not throw a warning in the php5 branch, only in php7. Mostly I was hoping someone would know if this will turn up as a fatal error in versions yet to come. Which I see as the only value in fixing this one now rather than when I get a white screen later. – pathfinder Dec 27 '19 at 23:37

Coding needs to be intentful - this warning is the compiler saying "are you sure you just want to create a default object out of nothing here?". Maybe you thought the variable had been assigned/initiated already, maybe you knew it wasn't and really do want to create a new default object, but without the line

$foo = new stdClass();

your intent is unclear. (Even better if you have a comment explaining why you're doing this - as in the purpose of $foo, not just that you want to stop the warning).

  • I think you meant $foo = new stdClass(); – pathfinder Dec 27 '19 at 23:28
  • Seems like this should be a Notice, rather than a Warning, especially if there is no issue in creating the Object and there is no plans to deprecate this action later down the road. – pathfinder Dec 27 '19 at 23:38
  • As for "no issue", that's probably opinion. Most people I know avoid using uninitialised variables. As for warning vs notice - that's a matter for the core team to choose between, and there is an inconsistency there since $x += 2; is "only" a notice. Then again, I prefer to not even have notices - again, being intentful with my code. – HorusKol Dec 28 '19 at 1:02
  • by "no issue" I meant no "Fatal Error" - and I generally agree and prefer to have no warnings or notices and enjoy opening a php.log that is always blank once code goes into production. This came up because I am updating an aging web app I wrote a decade ago for a client with little $$ and this particular warning will be rather costly in hours. In new code I do actually properly initialize new objects. – pathfinder Dec 28 '19 at 20:32

There are two distinct questions: One, what's the harm of creating a default object from an empty value? Obviously, no harm at all if that is what you intended. Possibly huge harm if it was not what you intended. Your judgement call.

Two: What's the harm in ignoring the warning? If you don't check that the code does what you intended, then ignoring the warning can be massively harmful (see answer One). If you checked that the behaviour is as intended, then the harm is that you have a warning now that will be shown on every single build, and that you will ignore on every single build, and it will make you ignore warnings that you shouldn't have ignored. So go and fix it.

(I worked on a project once that had been built for ages with all warnings disabled. Turning useful warnings on in the compiler produced about 3,000 warnings, of which 50-60 pointed to actual bugs. )

  • thank you for your thoughtful answer :) – pathfinder Dec 28 '19 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.