<?phpinclude_once  "Product.php";
class Cart {
    private $items = array();    public function getItems() {
        return $this->items;
    }
    public function addItem(Product $product,$num=1) {
        foreach($this->items as $item) {
            $p = $item->product;
            if($p->id == $product->id) {
                $item->count = $item->count + $num;
                return;
            }        }
        //新产品
        $item = new CartItem($product,$num);
        //print_r($item);
        $this->items[] = $item;
    }    public function remove($pid){
//     //echo $pid;
     for($i=0;$i<count($this->items);$i++){
$item=$this->items[$i];
$p=$item->product;
   //print_r($p);
if($p->id==$pid){
echo $i;
unset($this->items[$i]);
break;
}
}
}
    public function changeItemCount($pid,$count) {
        for($i=0;$i<count($this->items);$i++) {
            $item = $this->items[$i];
            $p = $item->product;
            if($p->id == $pid) {
                $item->count = $count;
                break;
            }
        }
    }
}
class CartItem {
    private $product; //产品
    private $count; //数量    public function __construct(Product $product,$count,$cart) {
        $this->product = $product;
        $this->count = $count;    }
    public function __get($n) {
        return $this->$n;
    }
    public function __set($n,$v) {
        $this->$n = $v;
    }}
?>
调用:
$cart->remove($_GET['key']);
  $_SESSION['cart'] = $cart;      if($_SESSION['cart']==null){
     echo "<a href='index.php'>您的购物车为空,请返回去重新选择商品</a>";
         exit();
      }
为什么有时删除不了?请高手解答