PHP |空函数是否可以使用对象属性?|简单的问题


PHP | empty function can work with object properties ? | Simple issue

我正在为一个非常特定的目的创建一种购物车,我已经创建了两个基本类。一个类用于描述购物车中的产品,另一个类是购物车。

我的CartItem类看起来是这样的:

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;
    }
    ...
}

我的购物车等级如下:

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;
    }
}

然后我试着玩这个结构:

$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);

但问题是,我得到了以下结果:

Is it empty : 1 : 25

上面的行打印在更新方法中的Cart类中。

正如你所看到的,我正在测试$item->qty是否为空,并且测试返回true,并且在相同的值中,我正在响应当前item的quanity,它是25,所以实际属性不为空。

我做错什么了吗?在PHP文档中,他们描述了函数empty可以用来测试变量中的空值或null值,但我不确定这在属性中是否有效。

有人能帮帮我吗?

我认为问题来自您的CartItem::__get()。我解释道:$qty是私有的,所以empty($item->qty)返回true,因为它在元素外部是不可访问的,但当您仅使用$item->qty访问它时,该变量将被视为不存在的(因为它是私有的),因此__get()方法将被调用,并将返回正确的值,因为您在类中访问它。