<!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 $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";
$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();