How to fix ‘Creating default object from empty value’ warning in PHP [duplicate]

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.

$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 387 with the following fix.

$commentsResult[$comment->type . '-' . $comment->contentid] = new StdClass;
$commentsResult[$comment->type . '-' . $comment->contentid]->_comment_count = 0;

Leave a Comment