interface IChargeable { public function getPrice(); } class ShopProduct implements IChargeable { private $title; private $producerMainName; private $producerFirstName; protected $price; function __construct($title, $firstName, $mainName, $price) { $this->title = $title; $this->producerMainName = $mainName; $this->producerFirstName = $firstName; $this->price = $price; } public function getPrice () { return $this->price; } public function getProducerMainName() { return $this->producerMainName; } public function getProducerFirstName() { return $this->producerFirstName; } public function setDiscount( $num ) { $this->discount=$num; } public function getDiscount() { return $this->discount; } public function getTitle() { return $this->title; } public function getProducer() { return "{$this->producerFirstName} {$this->producerMainName}"; } function getSummaryLine() { $base = "{$this->title} ({$this->producerMainName}, {$this->producerFirstName})"; return $base; } } class CDProduct extends ShopProduct { private $playLength = 0; function __construct ($title, $firstName, $mainName, $price, $playLength) { parent::__construct($title, $firstName, $mainName, $price); $this->playLength = $playLength; } function getPlayLength() { return $this->playLength; } function getSummaryLine() { $base = parent::getSummaryLine().": Продолжительность - {$this->playLength}"; return $base; } } class BookProduct extends ShopProduct { private $numPages = 0; function __construct ($title, $firstName, $mainName, $price, $numPages) { parent::__construct($title, $firstName, $mainName, $price); $this->numPages = $numPages; } function getNumberOfPages() { return $this->numPages; } function getSummaryLine() { $base = parent::getSummaryLine().": {$this->numPages} стр."; return $base; } } abstract class ShopProductWriter { protected $products = array(); public function addProduct(ShopProduct $shopProduct) { $this->products[] = $shopProduct; } abstract public function write(); } class XmlShopProductWriter extends ShopProductWriter{ public function write() { $str = ''."\n"; $str .= "\n"; foreach ( $this->products as $product ) { $str .= "\tgetTitle()}\">\n"; $str .= "\t\t\n"; $str .= "\t\t{$product->getSummaryLine()}\n"; $str .= "\t\t\n"; $str .= "\t\n"; } $str .= "\n"; print $str; } } class TextShopProductWriter extends ShopProductWriter{ public function write() { $str = "ТОВАРЫ: "; foreach ($this->products as $product) { $str .= "{$product->getTitle()}: {$product->getProducer()}, {$product->getPrice()}$
"; } echo $str; } }