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

WebTechRiser.com > Joomla 3.0 > How to fix ‘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 in my development PC, my test Joomla! site 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) implicitely. 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.

N.B. : Xampp 1.8.1 32-bit was using for my development environment.

Category Joomla 3.0

Leave a Reply

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