PHP 5.4:禁用警告“从空值创建默认对象”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13323993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP 5.4: disable warning "Creating default object from empty value"
提问by Werner
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:
我想将代码从 PHP 5.2 迁移到 5.4。到目前为止,这工作得很好,除了我使用的所有代码都广泛使用仅使用带有成员的对象而无需任何初始化,例如:
$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?!
我知道我可以将 php error_reporting 设置为不报告警告,但我希望仍然能够收到其他警告和通知。此警告似乎根本不受启用或禁用 E_STRICT 的影响。那么有没有办法禁用这个警告?!
采纳答案by Jon
Technically you coulddo this by installing your own error handlerfor warnings. From inside the handler check the string error message; if it's the one you want to suppress then return true, otherwise return falseto let the default error handler do its thing.
从技术上讲,您可以通过为警告安装自己的错误处理程序来做到这一点。从处理程序内部检查字符串错误消息;如果它是你想要抑制的那个return true,那么return false就让默认的错误处理程序做它的事情。
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.
但是,我仍然建议做正确的事情并在出现这种滥用的任何地方手动修复您的代码,因为如果不出意外,它会让您养成正确的习惯。除非这是有偿工作(在这种情况下,通常会担心会覆盖实施的纯度),请将其视为一个教训并做正确的事情。
回答by Wesley Murch
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.
我知道我可以将 php error_reporting 设置为不报告警告,但我希望仍然能够收到其他警告和通知。
Don't disable the error reporting, leave it at an appropriate level, but disable the display_errorsdirective:
不要禁用错误报告,将其保留在适当的级别,但禁用display_errors指令:
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:
然后正如 Jon 所说,如果您还没有使用自定义错误处理程序,例如:
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.
您可以将错误报告级别传递给set_error_handler. 通过这种方式,您可以花时间做正确的事情,即修复代码以停止生成通知。
If you're moving a lot of sites over to 5.4, you can probably have your server admin turn off display_errorsin 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.
如果您将许多站点迁移到 5.4,您可能可以display_errors在 php.ini 中关闭服务器管理员,直到您有足够的时间来修复所有内容。更好的是,在升级之前暂存所有内容并修复它,但这可能不是一种选择。
回答by Komdev
you really could use an IDE for php and try to do this:
你真的可以为 php 使用 IDE 并尝试这样做:
Search for $MyObject->MyMember = "Hello";
搜索 $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...
所以也许 IDE 会为你做长期的工作......
回答by dariush
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 handlerand with a proper regex and preg_matchignore the “Creating default object from empty value”warning and log/prompt others at your discretion.
我不得不说我的应用程序不需要/生成错误,它在一个非常受控的环境中,所以这样做不会有太大伤害,但是如果您的应用程序容易出错,您应该拥有自己的错误处理程序并具有适当的regex 和preg_match忽略“Creating default object from empty value”警告并根据您的判断记录/提示其他人。

