fork download
  1. <?php
  2.  
  3. class Library
  4. {
  5. private $items = [];
  6.  
  7. public function addInventory(IItem $item)
  8. {
  9. $this->items[] = $item;
  10. }
  11. }
  12. interface IItem
  13. {
  14. public function getTitle();
  15. }
  16. class Hardback implements IItem
  17. {
  18. private $title = null;
  19. private $author = null;
  20. public function __construct($title, $author)
  21. {
  22. $this->title = $title;
  23. $this->author = $author;
  24. echo "New Title added for Hardback\r\n";
  25. }
  26. public function getTitle()
  27. {
  28. return $thi->title;
  29. }
  30.  
  31. }
  32.  
  33.  
  34. class Softback implements IItem
  35. {
  36.  
  37. private $title = null;
  38. private $author = null;
  39. public function __construct($title, $author)
  40. {
  41. $this->title = $title;
  42. $this->author = $author;
  43. echo "New Title added for Softback\r\n";
  44. }
  45. public function getTitle()
  46. {
  47. return $thi->title;
  48. }
  49. }
  50.  
  51. class Movie implements IItem
  52. {
  53. private $title = null;
  54. private $length = null;
  55.  
  56. public function __construct($title, $length)
  57. {
  58. $this->title = $title;
  59. $this->lenght = $length;
  60. echo "New Movie Title added\r\n";
  61. }
  62. public function getTitle()
  63. {
  64. return $thi->title;
  65. }
  66.  
  67. }
  68.  
  69. $library = new Library();
  70. $inventoryItems = [
  71. new Hardback('Wind in the Willows (hb)', "Someone"),
  72. new Softback('Wind in the Willows (sb)', "Someone"),
  73. new Softback('Things that go bump (sb)', "Someone else"),
  74. new Movie('Max Payne', "120 min"),
  75. ];
  76.  
  77. foreach ($inventoryItems as $material) {
  78. $library->addInventory($material);
  79. }
  80.  
Success #stdin #stdout 0.01s 82560KB
stdin
Standard input is empty
stdout
New Title added for Hardback
New Title added for Softback
New Title added for Softback
New Movie Title added