如何修复 PHP 中的“从空值创建默认对象”警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14806959/
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
How to fix 'Creating default object from empty value' warning in PHP
提问by Mark Tomlin
I get the following error from a joomla install that I have.
我从我拥有的 joomla 安装中收到以下错误。
Warning: Creating default object from empty value in /modules/mod_continuous_rss_scrolling/helper.php on line 54
How do I fix this error?
我该如何解决这个错误?
回答by Mark Tomlin
As it turns out, the author missed a very simple fix and general good practice that you should always initialize your object before you try to set a property. The very simple fix for this is simply to add a new StdClass;call right before the error with the variable it is trying to access.
事实证明,作者错过了一个非常简单的修复方法和一般的良好实践,即您应该始终在尝试设置属性之前初始化您的对象。解决这个问题的非常简单的方法是new StdClass;在它试图访问的变量出现错误之前添加一个调用。
$items[$i] = new StdClass;
$items[$i]->title = $crs_post_title;
That first line will fix the warning from showing up.
第一行将修复出现的警告。
This would also fix the problem in /components/com_community/models/activities.php on line 387with the following fix.
这也将/components/com_community/models/activities.php on line 387通过以下修复解决问题。
$commentsResult[$comment->type . '-' . $comment->contentid] = new StdClass;
$commentsResult[$comment->type . '-' . $comment->contentid]->_comment_count = 0;

