脚本宝典收集整理的这篇文章主要介绍了如何在php中创建对象数组,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
这是将包含在数组中的类
<?PHP class hoteldetails { PRivate $hotelinfo; private $price; public function sethotelinfo($hotelinfo){ $this->hotelinfo=$hotelinfo; } public function setprice($price){ $this->price=$price; } public function gethotelinfo(){ return $hotelinfo; } public function getprice(){ return $price; } }
以下是我试图做的事情 –
<?PHP include 'file.PHP'; $hotelsdetail=array(); $hotelsdetail[0]=new hoteldetails(); $hotelsdetail[0].sethotelinfo($rs); $hotelsdetail[0].setprice('150'); ?>
尝试创建数组的类不能编译,但只是对如何执行此操作的最佳猜测.再次感谢
$hotelsDetail = array(); $details = new HotelDetails(); $details->setHotelInfo($rs); $details->setPrice('150'); // assign IT to the array here; you don't need the [0] index then $hotelsDetail[] = $details;
在您的具体情况下,问题是您应该使用 – >,而不是.. PHP中不使用句点来访问类的属性或方法:
$hotelsdetail[0] = new hoteldetails(); $hotelsdetail[0]->sethotelinfo($rs); $hotelsdetail[0]->setprice('150');
以上是脚本宝典为你收集整理的如何在php中创建对象数组全部内容,希望文章能够帮你解决如何在php中创建对象数组所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。