php5 OOP - 对象类型限制不起作用

时间:2013-08-08 19:32:51

标签: php oop

我正在学习OOP,并且在不理解代码的情况下遇到了一些问题。

在这里。

class ShopProduct {

    private $title;
    private $producerMainName;
    private $producerFirstName;
    protected $price;
    private $discount = 0;


    function __construct( $name, $firstName, $mainName, $price) {
        $this->title = $name;
        $this->producerFirstName = $firstName;
        $this->producerMainName = $mainName;
        $this->price = $price;
    }

    public function getProducer() {
        return "{$this->producerMainName} "."{$this->producerFirstName} \n ";
    }

    public function setDiscount($num){
        $this->discount = $num;
    }

    public function getDiscount() {
        return $this->discount;
    }

    public function getTitle() {
        return $this->title;
    }

    public function getPrice() {
        return ($this->price - $this->discount);
    }

    public function getSummaryLine() {
        $base = "{$this->title} ( {$this->producerMainName}, ";
        $base .= "{$this->producerFirstName} )";
        return $base;
    }
}

class CDProduct extends ShopProduct {
    private $playLength = 0;

    public function __construct($title, $firstName, $mainName, $price, $playLength) {
        parent::__construct($title, $firstName, $mainName, $price);
        $this->playLength = $playLength;
    }

    public function getPlayLength() {
        return $this->playLength;
    }

    public function getSummaryLine() {
        $base = parent::getSummaryLine();
        $base .= ": {$this->playLength()} minutes";
        return $base;
    }

}
class BookProduct extends ShopProduct {

    private $numPages = 0;

    public function __construct($title, $firstName, $mainName, $price, $numPages) {
        parent::__construct($title, $firstName, $mainName, $price);
        $this->numPages = $numPages;
    }


    public function getNumberOfPages() {
        return $this->numPages;
    }

    public function getSummaryLine() {
        $base = parent::getSummaryLine();
        $base .= ": {$this->numPages()} pages";
        return $base;
    }
}
class ShopProductWriter {

    private $products = array();
    public function addProduct($shopProduct){
        if(! ($shopProduct instanceof ShopProduct) ){
        die('object error');
        }
        $this->products[] = $shopProduct;
    }
    public function write($shopProduct) {   
        foreach($this->products as $shopProducts){
            $str = "{$shopProduct->getTitle()}: "."{$shopProduct->getProducer()}"." {$shopProduct->getPrice()}$ \n";
        }
        print $str;
    }
}
$product = new CDProduct('Song is the rythm','Zyxel','Beatz',50, 60.33);
$write = new ShopProductWriter();
$write->addProduct($product);
$write->write($product);

问题出在这里

class ShopProductWriter {

        private $products = array();
        public function addProduct($shopProduct){
            if(! ($shopProduct instanceof ShopProduct) ){
            die('object error');
            }
            $this->products[] = $shopProduct;
        }
        public function write($shopProduct) {   
            foreach($this->products as $shopProducts){
                $str = "{$shopProduct->getTitle()}: "."{$shopProduct->getProducer()}"." {$shopProduct->getPrice()}$ \n";
            }
            print $str;
        }
    }

如您所见,有条件 - 如果对象不是ShopProduct类型 - 则会出错。 但正如您所看到我正在创建CDProduct对象。

$product = new CDProduct('Song is the rythm','Zyxel','Beatz',50, 60.33);
$write = new ShopProductWriter();
$write->addProduct($product);
$write->write($product);

它应该显示错误。任何人都可以说我做错了什么?

2 个答案:

答案 0 :(得分:4)

CDProduct的对象也是ShopProduct类型。在类定义中:

class CDProduct extends ShopProduct {

因此它是两种类型的对象。

http://php.net/manual/en/language.oop5.inheritance.php

如果对象扩展父对象或实现接口,则也可以考虑该类型。

http://php.net/manual/en/language.operators.type.php

答案 1 :(得分:0)

您已经有关于CDProductShopProduct的原因的反馈意见,所以我只是添加了一些您已编写的已经支持的代码的提示:

    public function addProduct($shopProduct){
        if(! ($shopProduct instanceof ShopProduct) ){
        die('object error');
        }
        $this->products[] = $shopProduct;
    }

您可以使用Type Hinting来减少代码,使其更具表现力,而不是自行检查:

    public function addProduct(ShopProduct $shopProduct) {
        $this->products[] = $shopProduct;
    }

我希望这有助于你写下你目前正在学习的OOP。