lopes_andre Posted August 28, 2009 Share Posted August 28, 2009 Hi, I'am reading the book "PHP Objects, Patterns and Practice" and one of the examples in the book is this one: <?php class ShopProductWriter { public $products = array(); public function addProduct( ShopProduct $shopProduct ) { $this->products[] = $shopProduct; } public function write() { $str = ""; foreach ( $this->products as $shopProduct ) { $str .= "{$shopProduct->title}: "; $str .= $shopProduct->getProducer(); $str .= " ({$shopProduct->getPrice()})\n"; } print $str; } } ########################################## class ShopProduct { private $title; private $producerMainName; private $producerFirstName; protected $price; private $discount = 0; public function __construct( $title, $firstName, $mainName, $price ) { $this->title = $title; $this->producerFirstName = $firstName; $this->producerMainName = $mainName; $this->price = $price; } public function getProducerFirstName() { return $this->producerFirstName; } public function getProducerMainName() { return $this->producerMainName; } 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 getProducer() { return "{$this->producerFirstName}". " {$this->producerMainName}"; } 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 .= ": playing time - {$this->playLength}"; 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 .= ": page count - {$this->numPages}"; return $base; } public function getPrice() { return $this->price; } } $produto = new ShopProduct("AA", "BB", "CC", "0"); echo $produto->getTitle(); ?> My question is: What does the class "ShopProductWriter" and how use it. In the book I don't see any explanation on on to use this class. Best Regards, André. Quote Link to comment Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 ShopProductWriter writes out your cart items. ShopProduct represents a product. You would use it like: $spw = new ShopProductWriter(); $spw->addProduct(new ShopProduct('Product #1', 'Firstname', 'MainName', 'expensive')); $spw->addProduct(new ShopProduct('Product #2', 'Firstname', 'MainName', 'expensive')); $spw->addProduct(new ShopProduct('Product #3', 'Firstname', 'MainName', 'expensive')); $spw->addProduct(new ShopProduct('Product #4', 'Firstname', 'MainName', 'expensive')); print $spw->write(); I won't recommend using it on a production server (like it allows 'expensive' as a price..) though plus there are many things this class does not make advantage of (__toString(), ..). Quote Link to comment Share on other sites More sharing options...
lopes_andre Posted August 29, 2009 Author Share Posted August 29, 2009 Thks, It worked! I will not use this code in production. The code is only for educational purposes. Best Regards, André. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted August 29, 2009 Share Posted August 29, 2009 Make sure you mark this post as solved if someone has answered your question :-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.