Ale*_*les 6 php arrays oop object
我正在尝试在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;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我试图做的事情 -
<?PHP
include 'file.php';
$hotelsdetail=array();
$hotelsdetail[0]=new hoteldetails();
$hotelsdetail[0].sethotelinfo($rs);
$hotelsdetail[0].setprice('150');
?>
Run Code Online (Sandbox Code Playgroud)
尝试创建数组的类不能编译,但只是对如何执行此操作的最佳猜测.再次感谢
slh*_*hck 18
你应该做的是:
$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;
Run Code Online (Sandbox Code Playgroud)
在您的具体情况下,问题是您应该使用->,而不是..PHP中不使用该句点来访问类的属性或方法:
$hotelsdetail[0] = new hoteldetails();
$hotelsdetail[0]->sethotelinfo($rs);
$hotelsdetail[0]->setprice('150');
Run Code Online (Sandbox Code Playgroud)
请注意,我正确地将类,对象和函数名称大写.用小写写一切都不算好风格.
作为旁注,为什么你的价格是一个字符串?如果你想用它进行适当的计算,它应该是一个数字.