$str .= "\t<product title=\"{$shopProduct->getTitle()}\">\n"; $str .= "\t\t<summary>\n"; $str .= "\t\t{$shopProduct->getSummaryLine()}\n"; $str .= "\t\t</summary>\n"; $str .= "\t</product>\n"; } $str .= "</products>\n"; print $str; } } class TextProductWriter extends ShopProductWriter { public function write() { $str = "PRODUCTS:\n"; foreach ($this->products as $shopProduct) { $str .= $shopProduct->getSummaryLine() . "\n"; } print $str; } } $product1 = new BookProduct("My Antonia", "Willa", "Cather", 5.99, 300); $product2 = new CdProduct("Exile on Coldharbour Lane", "The", "Alabama 3", 10.99, 60.33); $textwriter = new TextProductWriter(); $textwriter->addProduct($product1); $textwriter->addProduct($product2); $textwriter->write(); $xmlwriter = new XmlProductWriter(); $xmlwriter->addProduct($product1); $xmlwriter->addProduct($product2); $xmlwriter->write();
// summary $writer->endElement(); // product } $writer->endElement(); // products $writer->endDocument(); print $writer->flush(); } } class TextProductWriter extends ShopProductWriter { public function write() { $str = "PRODUCTS:\n"; foreach ($this->products as $shopProduct) { $str .= $shopProduct->getSummaryLine() . "\n"; } print $str; } } $music = new XmlProductWriter(); $eminem = new ShopProduct('Marshal Matters', 'Slim', 'Shady', 300); $pod = new ShopProduct('youth of a nation', 'POD', '-', 250); $music->addProduct($eminem); $music->addProduct($pod); $music->write(); ?> </body> </html>