PHP 5.4:禁用警告“从空值创建默认对象"; [英] PHP 5.4: disable warning "Creating default object from empty value"

查看:77
本文介绍了PHP 5.4:禁用警告“从空值创建默认对象";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将代码从PHP 5.2迁移到5.4.到目前为止,这一切工作正常,除了我使用的所有代码都广泛使用了仅将对象与成员一起使用而未进行任何初始化的情况,例如:

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.

我知道我可以将php error_reporting设置为不报告警告,但是我希望仍然能够获得其他警告和通知.此警告似乎完全不受启用或禁用E_STRICT的影响.有没有办法禁用此警告?!

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?!

推荐答案

技术上,您可以通过

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.

这篇关于PHP 5.4:禁用警告“从空值创建默认对象";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆