【发布时间】:2014-07-07 06:18:12
【问题描述】:
我正在尝试序列化我的对象“ShoppingCart.php”和对象“Item.php”,但我不能,而且我不知道还能做什么,所以我把我的代码留在这里看看是否有人可以帮我。我的问题是对象 ShoppingCart.php 有一个存储项目数组的属性,我不确定我是否很好地序列化/反序列化对象。
我已经总结了我的类,所以它们不包括所有方法,但它们仍然显示了最多的代表。如果还不够清楚,我会再次更改它。
提前致谢:
<?php
class ShoppingCart implements Iterator, Countable,Serializable {
// Array stores the list of items in the cart:
protected $items = array();
// For tracking iterations:
protected $position = 0;
// For storing the IDs, as a convenience:
protected $ids = array();
// Constructor just sets the object up for usage:
function __construct() {
$this->items = array();
$this->ids = array();
}
// Adds a new item to the cart:
public function addItem(Item $item) {
// Need the item id:
$id = $item->getId();
// Throw an exception if there's no id:
if (!$id) throw new Exception('The cart requires items with unique ID values.');
// Add or update:
if (isset($this->items[$id])) {
$this->updateItem($item, $this->items[$id]['qty'] + 1);
} else {
$this->items[$id] = array('item' => $item, 'qty' => 1);
$this->ids[] = $id; // Store the id, too!
}
} // End of addItem() method.
// Changes an item already in the cart:
public function updateItem(Item $item, $qty) {
// Need the unique item id:
$id = $item->getId();
// Delete or update accordingly:
if ($qty === 0) {
$this->deleteItem($item);
} elseif ( ($qty > 0) && ($qty != $this->items[$id]['qty'])) {
$this->items[$id]['qty'] = $qty;
}
} // End of updateItem() method.
// Removes an item from the cart:
public function deleteItem(Item $item) {
// Need the unique item id:
$id = $item->getId();
// Remove it:
if (isset($this->items[$id])) {
unset($this->items[$id]);
// Remove the stored id, too:
$index = array_search($id, $this->ids);
unset($this->ids[$index]);
// Recreate that array to prevent holes:
$this->ids = array_values($this->ids);
}
} // End of deleteItem() method.
public function serialize(){
foreach ($this->items as $clave => $item)
$listaItems[$clave]=serialize($this->items['item'][$clave]);
foreach ($this-items as $clave=>$valor)
$listaCantidades[$clave]=$this->items['qty'][$clave];
return
array(
'items'=>$listaItems,
'qty'=>$listaCantidades,
'ids'=>$this->ids,
);
}
public function unserialize($data){
//$data=unserialize($data);
$this->items=array(
'items'=>$data['items'],
'qty'=>$data['qty']
);
$this->ids=$data['ids'];
foreach($this->items as $clave => $item)
$this->items[$clave]=unserialize($item);
}
} // End of ShoppingCart class.
<?php # Item.php
// This is a sample Item class.
// This class could be extended by individual applications.
class Item implements Serializable{
// Item attributes are all protected:
protected $id;
protected $name;
protected $price;
protected $description;
public function serialize(){
return serialize(
array(
'id'=>$this->id,
'name'=>$this->name,
'price'=>$this->price,
'description'=>$this->description
)
);
}
public function unserialize($data){
$data=unserialize($data);
$this->id=$data['id'];
$this->name=$data['name'];
$this->price=$data['price'];
$this->description=$data['description'];
}
// Constructor populates the attributes:
public function __construct($id, $name, $price) {
$this->id = $id;
$this->name = $name;
$this->price = $price;
}
// Method that returns the ID:
public function getId() {
return $this->id;
}
// Method that returns the name:
public function getName() {
return $this->name;
}
// Method that returns the price:
public function getPrice() {
return $this->price;
}
public function getDescription() {
return $this->description;
}
public function setDescription($descripcion){
$this->description=$descripcion;
}
} // End of Item class.
【问题讨论】:
-
您应该删除所有不必要的代码并准确描述您的问题。如果您只想进行代码审查,请转到 codereview.stackexchange.com
-
好的,谢谢你的建议,我刚刚编辑过,但我仍然包含一些方法,因为我很感兴趣,你可以看看数组是如何工作的。
标签: php session serialization