app \ Cart :: __ construct():脚本试图执行方法或访问不完整对象的属性

时间:2018-11-10 20:52:25

标签: php

<?php

namespace app;

class Cart
{

    public $items = null;
    public $totalqty = 0;
    public $totalprice = 0;

    public function __construct($oldCart){

        if($oldCart){

            $this->items = $oldCart->items;
            $this->totalqty = $oldCart->totalqty;
            $this->totalprice = $oldCart->totalprice;

        }
    }

    public function add($item,$id){     


        $storedItem = ['qty'=> 0, 'price' => $item->price,'item'=>$item];
        if($this->items){
            if(array_key_exists($id, $this->items)){
                $storedItem = $this->items[$id];
            }
        }

        $storedItem['qty']++;
        $storedItem['price'] = $item->price * $storedItem['qty'];
        $this->items[$id] = $storedItem;
        $this->totalqty++;
        $this->totalprice+=$item->price;
    }

  }

?>

0 个答案:

没有答案