我在php中創建一個cart bean對象,然後將它存儲在會話中當我試圖從會話訪問對象數組時,它總是隻返回一個bean對象 Here是bean類如何訪問會話中的bean對象__PHP_Incomplete_Class對象
Class Cart{
private $quanity;
private $amount;
function getQuantity() {
return $this->quantity;
}
function getAmount() {
return $this->amount;
}
function setQuantity($quantity) {
$this->quantity = $quantity;
}
function setAmount($amount) {
$this->amount = $amount;
}
AddToCart.php
$c = new cart();
$c->setAmount("500");
$c->setQuantity("10");
if(isset($_SESSION["cartArray"])){
$ar = $_SESSION["cartArray"];
$ar[]=$c;
$_SESSION["cartArray"]=$ar;
}
else{
$c;
$_SESSION['cartArray']= [];
$_SESSION['cartArray'][]=$c;
}
orderHandler.php
$ar = $_SESSION["cartArray"];
foreach ($ar as $value) {
echo '<pre>';
print_r($value);
echo '</pre>';
}
它給了我__PHP_Incomplete_Class對象 如果嘗試編寫以下行環路
$echo $value->getAmount();
錯誤:
Fatal error: main(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "cart" of the object you are trying to operate on was loaded before unserialize() gets called or provide a __autoload() function to load the class definition
在打開會話之前,您需要確保會話中存在的所有類都已加載並可用。 – 2015-02-11 09:02:45