PHP 5.4: disable warning “Creating default object from empty value”

僤鯓⒐⒋嵵緔 提交于 2019-11-28 10:03:15

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.

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.

Komdev

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...

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!