Php 从空值创建默认对象
在XAMPP上使用最新版本时,我在joomla上遇到以下警告:Php 从空值创建默认对象,php,joomla,xampp,Php,Joomla,Xampp,在XAMPP上使用最新版本时,我在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 通常这是通过新变量的显式定义来修复的,但是这次我找不到要修改什么来修复这个警告 $pickoneArray = array(); $pickone = ran
Warning: Creating default object from empty value in F:\xampp\htdocs\modules\mod_random_image_with_fancy_zoom\tmpl\default.php on line 26
通常这是通过新变量的显式定义来修复的,但是这次我找不到要修改什么来修复这个警告
$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 . "/";
这是print\u r的输出:
stdClass Object
(
[name] => pic001.jpg
[folder] => images\images\pic_of_the_day\
)
正如您所看到的,$pickoneArray没有要显示的内容。
$pickoneArray[0]
什么都没有,因此,$pickoneArray[0]->name=…
创建了一个默认对象。您应该明确地创建它:
$pickoneArray[0] = new stdClass;
$pickoneArray[0]->name = ...
但是,由于您只是将对象属性重新指定给另一个对象中的相同属性,这将更有意义:
$pickoneArray[0] = $images[$pickone];
事实上,这一切可以归结为:
$pickoneArray = array($images[array_rand($images)]);
但在这里,我想问你为什么需要一个数组。为什么不简单地:
$pickone = $images[array_rand($images)];
为什么不能将第一行变成
$pickoneArray=array(newstdclass())代码>?