10

I want to migrate code from PHP 5.2 to 5.4. This worked fine so far except that all the code I use makes extensive use of just using an object with a member without any initialisation, like:

$MyObject->MyMember = "Hello";

which results in the warning: "Creating default object from empty value"

I know that the solution would be to use:

$MyObject = new stdClass();
$MyObject->MyMember = "Hello";

but it would be A LOT OF WORK to change this in all my code, because I use this many times in different projects. I know, it's not good style, but unfortunately I'm not able to spend the next weeks adding this to all of my code.

I know I could set the php error_reporting to not reporting warnings, but I want to be able to still get other warnings and notices. This warning doesn't seem to be effected by enable or disable E_STRICT at all. So is there a way to just disable this warning?!

3
  • I gotta say, I didn't know about this in 5.4 - looks like I'll be in the same boat as you soon enough. Commented Nov 10, 2012 at 17:10
  • Yeah - sometime you will not be able to write new software because you have to upgrade to old stuff... Commented Nov 14, 2012 at 15:55
  • 2
    This warning should have stayed E_STRICT and not become E_WARNING! Seems that PHP developers are constantly inventing new ways to make our life difficult. Commented Nov 20, 2013 at 6:12

4 Answers 4

8

Technically you could do this by installing your own error handler for warnings. From inside the handler check the string error message; if it's the one you want to suppress then return true, otherwise return false to let the default error handler do its thing.

However I would still recommend doing the right thing and manually fixing your code wherever this misuse does appear because, if nothing else, it gets you into the correct habit. Unless this is paid work (in which case there usually are concerns that override purity of implementation), consider this as a lesson and do the right thing.

Sign up to request clarification or add additional context in comments.

1 Comment

I was afraid someone would answer like this ;-) Yeah, you are fully right, lets see if I find the time.
4

I know I could set the php error_reporting to not reporting warnings, but I want to be able to still get other warnings and notices.

Don't disable the error reporting, leave it at an appropriate level, but disable the display_errors directive:

ini_set('display_errors', 0);

There's no reason to print notices to the screen in production.

Then as Jon said, use a custom error handler if you aren't already, example:

function my_error_handler($error_level, $error_message)
{
    // write error message to log file
}
set_error_handler('my_error_handler');

You can pass in the error reporting level to the second param of set_error_handler. This way you can take your time and do the right thing, which is fix the code to stop generating notices.

If you're moving a lot of sites over to 5.4, you can probably have your server admin turn off display_errors in php.ini until you have enough time to fix everything. Better yet, stage everything and fix it before upgrading, but that may not be an option.

Comments

2

you really could use an IDE for php and try to do this:

Search for $MyObject->MyMember = "Hello";

Look for the results and if it brings the right things, try to replace that with:

$MyObject = new stdClass();
$MyObject->MyMember = "Hello";

So maybe the IDE would do the long work for you...

2 Comments

This is a big mistake!! What if the $MyObject already exists! You must first check it: if (!isset($MyObject)) $MyObject = new stdClass();
I have a few locations i my project where $MyObject = new stdClass(); $MyObject->MyMember = "Hello"; is still giving the error message. I have no idea why.
0

I had the same problem i handle it like bellow in production environment

    \error_reporting(\E_ERROR);

I have to say that my application doesn't need/generate errors, it's on a very controlled environment so doing this wouldn't hurt much, but if your application is an error prone one you should have you own error handler and with a proper regex and preg_match ignore the “Creating default object from empty value” warning and log/prompt others at your discretion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.