Search code examples
phpjoomlajoomla2.5php-5.5

Fixing 'Creating default object from empty value' warning in PHP


I recently uploaded a Joomla 2.5 site from my dev server to our host and discovered that one of the extensions that is a part of our site uses PHP code that is no longer "acceptable" in PHP 5.5. I keep getting the

Warning: Creating default object from empty value .../helper.php on line 36

Since it is only one error I'm getting, I'd like to fix the php instead of simply hiding the warning.The relevant code line generating the error is:

$lists[$i]->id = $row->id;

I am aware that I should add a new StdClass; call right before the error with the variable it is trying to access per Mark Tomlin's response in this post How to fix 'Creating default object from empty value' warning in PHP

However, being a php newb I'm not sure how that would look in the code below.

{
public static function getList($params)
{

    $items = modArticlesLatestHelper::getList($params);

    $text_length = intval($params->get( 'preview_count', 200) );
    $tags       = $params->get('strip_tags', "a,i,br");

    $i=0;
    $lists  = array();
    foreach ( $items as $row )
    {
        //process content plugins
        $text = JHTML::_('content.prepare',$row->introtext);
        $lists[$i]->id = $row->id;
        $lists[$i]->thumb = self::getThumb($row->introtext,$params->get('thumb_width',160));
        $lists[$i]->created = $row->created;
        $lists[$i]->modified = $row->modified;
        $lists[$i]->link = $row->link;
        $lists[$i]->title = htmlspecialchars( $row->title );
        $lists[$i]->introtext = self::prepareContent( $text, $text_length, $tags);
        $i++;
    }

    return $lists;
}

Thanks in advance for your suggestions, ideas and help.


Solution

  • Create object before setting property

    $lists[$i] = new stdClass();
    $lists[$i]->id = $row->id;
    

    Or single line solution

    $lists[$i] = (object)array('id' => $row->id);