0

I am creating a kind of shopping cart for a very specific purpose, and I have create two basic classes. The one class is made to describe a product in the cart, and the other class is the cart.

My CartItem class looks like that:

class CartItem
{
    private $id         =   null;        //  This is the product id
    private $qty        =   0;           //  This is the product Quantity
    private $price      =   0;           //  This is the product price
    private $name       =   '';          //  This is the product name
    private $options    =   array();     //  This array contains the product options
    private $rowid      =   null;        //  This is the product id in the cart
    private $subtotal   =   0;           //  This is the product sub total

    public function __construct(
        $id = null, 
        $qty = null, 
        $price = null, 
        $name = null, 
        $options = array()
    )
    {
        $this->id       =   $id;            
        $this->qty      =   (float)$qty;
        $this->price    =   (float)$price;
        $this->name     =   $name;
        $this->options  =   $options;
    }

    public function __get($name)
    {
        return $this->{$name};
    }

    ...

    public function setQty($qty = 0)
    {
        $this->qty      =   (float)$qty;
        return $this->qty;
    }

    ...

}

and my Cart class is the following:

class Cart
{
    protected $cart_contents    =   array();
    protected $cart             =   null;

    protected function __construct()
    {
        $this->cart             =   new SessionContainer('cart_contents');
        $this->cart_contents    =   $this->cart->offsetGet('contents');

        if(count($this->cart_contents)  <=  2)
        {
            $this->cart_contents    =   array(
                'cart_total'    =>  0,
                'total_items'   =>  0
            );
        }
    }

    public function insert(CartItem $item)
    {
        $save_cart  =   false;

        if(($rowid  =   $this->_insert($item)))
        {
            $save_cart  =   true;
        }

        if($save_cart   ===    true)
        {
            $this->_save_cart();

            return isset($rowid) ? $rowid : true;
        }

        return false;
    }

    protected function _insert(CDOCartItem $item)
    {
        if($item->qty   ==  0)
        {
            return false;
        }

        if(is_array($item->options) && count($item->options) > 0)
        {
            $rowid  =   md5($item->id . serialize($item->options));
        }
        else
        {
            $rowid  =   md5($item->id);
        }

        $old_quantity   =   isset($this->cart_contents[$rowid]->qty) ? $this->cart_contents[$rowid]->qty : 0;

        $item->setRowId($rowid);
        $item->setQty($item->qty + $old_quantity);

        $this->cart_contents[$rowid]    =   $item;
        return $rowid;
    }

    public function update(CDOCartItem $item)
    {
        $save_cart  =   false;

        if($this->_update($item) === true)
        {
            $save_cart  =   true;
        }

        if($save_cart === true)
        {
            $this->_save_cart();
            return true;
        }

        return false;
    }

    protected function _update(CartItem $item)
    {
        echo "Is this empty : " . empty($item->qty) . " : " . $item->qty;

        if(empty($item->qty) || empty($item->rowid) || !isset($this->cart_contents[$item->rowid]))
        {
            return false;
        }

        $item->setQty((float)$item->qty);

        if($item->qty    <=  0)
        {
            unset($this->cart_contents[$item->rowid]);
        }
        else
        {
            $this->cart_contents[$item->rowid]->qty = $item->qty;
        }

        return true;
    }
}

Then I try to play with this structure:

$item    =     new CartItem('SKU-12321', 12, 47.82, 'Name');
$cart    =     Cart::Instance(); // I have apply the singleton patternt in my local files
$itemRID =     $cart->insert($item);
$newItem =     $cart->get_item($itemRID); // This method return the object based on ID and 100% works in my code
$newItem->setQty(25);
$cart->update($newItem);

But the problem is that I am getting the following result:

Is it empty : 1 : 25

The above line is printed in the Cart Class in the update method.

As you can see, I am testing if the $item->qty is empty, and the test returns true, and in the same value I am echoing the current item quanity that it is 25 so, the actual property it is not empty.

Am I doing something wrong ? In PHP Documentation they are describing that the function empty can be used to test for empty or null values in variables, but I am not sure if that works in properties.

Can somebody to help me please ?

1 Answer 1

3

I think the problem comes from your CartItem::__get(). I explain: $qty is private, so a empty($item->qty) returns true because it is not accessible outside the element, but when you access it with just $item->qty, the variable will be considered as unexisting (because it is private) so the __get() method will be called and will return the right value because you access it within the class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.