VSB
VSB

Reputation: 10375

Creating default object from empty value

Using latest version on XAMPP i faced below warning on my joomla:

Warning: Creating default object from empty value in F:\xampp\htdocs\modules\mod_random_image_with_fancy_zoom\tmpl\default.php on line 26

usually this was fixed by explicit definition of new variables, however this time i could not find what to be changed to fix this warning?

$pickoneArray = array();
$pickone = rand(0, count($images)-1);
print_r($pickoneArray);
print_r($images[$pickone]);
$pickoneArray[0]->name = $images[$pickone]->name; //this is line 26
$pickoneArray[0]->folder = $images[$pickone]->folder . "/";

and here is the output of print_r:

stdClass Object
(
    [name] => pic001.jpg
    [folder] => images\images\pic_of_the_day\
)

as you can see $pickoneArray has nothing to be displayed.

Upvotes: 0

Views: 184

Answers (1)

deceze
deceze

Reputation: 522165

$pickoneArray[0] is nothing, so $pickoneArray[0]->name = ... creates a default object.
You should create it explicitly:

$pickoneArray[0]       = new stdClass;
$pickoneArray[0]->name = ...

But since you're just reassigning object properties to identical properties in another object, this would make a lot more sense:

$pickoneArray[0] = $images[$pickone];

In fact, this could all be boiled down to:

$pickoneArray = array($images[array_rand($images)]);

But here I'd question why you need an array at all. Why not simply:

$pickone = $images[array_rand($images)];

Upvotes: 2

Related Questions