public $numPages; function __construct($title, $firstName, $mainName, $price, $numPages) { parent::__construct($title, $firstName, $mainName, $price); $this->numPages = $numPages; } function getPrice() { return $this->price; } function getNumberOfPages() { return $this->numPages; } function getSummaryLine() { $base = parent::getSummaryLine(); $base .= ": page count - {$this->numPages}"; return $base; } } $product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50); $product1->setDiscount(3); print $product1->getSummaryLine(); print "\n"; print "price: {$product1->getPrice()}\n"; $product2 = new BookProduct("book1", "harry", "harrelson", 4, 30); $product2->setDiscount(3); print $product2->getSummaryLine(); print "\n"; print "price: {$product2->getPrice()}\n";
{ return $this->playLength; } function getSummaryLine() { $base = "{$this->title} ( {$this->producerMainName}, "; $base .= "{$this->producerFirstName} )"; $base .= ": playing time - {$this->playLength}"; return $base; } } class BookProduct extends ShopProduct { function getNumberOfPages() { return $this->numPages; } function getSummaryLine() { $base = "{$this->title} ( {$this->producerMainName}, "; $base .= "{$this->producerFirstName} )"; $base .= ": page count - {$this->numPages}"; return $base; } } $product1 = new CdProduct("cd1", "bob", "bobbleson", 4, null, 50); print $product1->getSummaryLine(); print "\n"; $product2 = new BookProduct("book1", "harry", "harrelson", 4, 30); print $product2->getSummaryLine(); print "\n";
<!doctype html> <html> <head> <meta charset="utf-8"> <title>Test Page for ShopProduct Class</title> </head> <body> <h3>Test Page for ShopProduct Class</h3> <?php spl_autoload_register(function ($class) { require $class . '.php'; }); // Instantiate some objects $cd1 = new CdProduct('The Boss', 'Diana', 'Diana Ross', 9.99, '60 minutes'); $book1 = new BookProduct('Black Genius', 'Dick', 'Dick Russell', 15.99, 375); echo '<p><b>Summary Line</b></p>'; echo $cd1->getSummaryLine() . '<br>'; echo $book1->getSummaryLine() . '<br>'; echo '<p><b>Summary Line (ShopProductWriter)</b></p>'; $writer = new TextProductWriter(); $writer->addProduct($cd1); $writer->addProduct($book1); echo nl2br($writer->write()); ?> </body> </html>
public function getSummaryLine() { $base = parent::getSummaryLine(); $base .= ": playing time - {$this->playLength}"; return $base; } } class BookProduct extends ShopProduct { public $numPages; 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; } } $product1 = new CdProduct("Exile on Coldharbour Lane", "The", "Alabama 3", 10.99, null, 60.33); print "artist: {$product1->getProducer()}\n"; echo "<hr>"; $product2 = new BookProduct("My Antonia", "Willa", "Cather", 5.75, null, 20.23); print "artist: {$product2->getProducer()}\n";
{ $base = parent::getSummaryLine(); $base .= ": playing time - {$this->playLength}"; echo $base; return $base; } } class BookProduct extends ShopProduct { public $numPages; 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(); $base .= ": page count - {$this->numPages}"; echo $base . "<br>"; return $base; } } $book = new BookProduct("Master and Margaruta", "Mihail", "Bulgakov", "45\$", "456"); $shakira = new CdProduct("My Mister", "NoName", "Shakira", "25\$", "56:01"); $book->getSummaryLine(); $shakira->getSummaryLine();