0

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.

1
  • 1
    Why can't you just turn the first line into $pickoneArray = array(new StdClass());? Commented Aug 13, 2014 at 11:29

1 Answer 1

2

$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)];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.