How to fix the ‘Creating default object from empty value’ PHP warning in Joomla

WebTechRiser.com > Joomla 3.0 > How to fix the ‘Creating default object from empty value’ PHP warning in Joomla

I was developing a Joomla! “Module” where I had written the following lines of codes in my “helper.php” file:

$i = 0;
$lists[] = array();

foreach($files as $file)
{
    // This line was triggering Waring Message
    $lists[$i]->image = JPath::clean( $path.'/'.$file, '/' );
    $i++;
}

While testing this module on my development PC, my Joomla site on localhost was giving me the following error:

Warning: Attempt to assign property of non-object in
D:\xampp\htdocs\j315\modules\mod_newsschedule\helper.php on line 51

The error occurs because $lists[$i] has not been defined yet as an object and PHP instantiated a default object (of type stdClass) implicitly. To solve this problem I just rewrite the line and my problem was solved.

$lists[$i] = new stdClass();

My final code looked like the following and the warning message was gone:

i = 0;
$lists[] = array();

foreach($files as $file) {
    $lists[$i] = new stdClass();
    $lists[$i]->image = JPath::clean( $path.'/'.$file, '/' );
    $i++;
}

Hope this helps.

Nota Bene: I was using Xampp 1.8.1 32-bit on Windows 7.

Also read:  Get any property of the active menu item class in Joomla! 3
Category Joomla 3.0

Leave Your Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.