Advertisement
vvccs

wt_9_advanced-oops

Oct 13th, 2024 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.84 KB | None | 0 0
  1. <?php
  2.  
  3. // Abstract class for Media items
  4. abstract class Media {
  5.     protected $title;
  6.     protected $author;
  7.     const MEDIA_TYPE = 'General';
  8.  
  9.     // Constructor to initialize title and author
  10.     public function __construct($title, $author) {
  11.         $this->title = $title;
  12.         $this->author = $author;
  13.     }
  14.  
  15.     // Abstract method to get details of the media
  16.     abstract public function getDetails();
  17.  
  18.     // Static method to get media type
  19.     public static function getMediaType() {
  20.         return static::MEDIA_TYPE;
  21.     }
  22. }
  23.  
  24. // Interface for borrowable items
  25. interface Borrowable {
  26.     public function borrow($borrower);
  27. }
  28.  
  29. // Class for Book, extending Media and implementing Borrowable
  30. class Book extends Media implements Borrowable {
  31.     private $isbn;
  32.     public static $totalBooks = 0;
  33.     const MEDIA_TYPE = 'Book';
  34.  
  35.     // Constructor to initialize title, author, and ISBN
  36.     public function __construct($title, $author, $isbn) {
  37.         parent::__construct($title, $author);
  38.         $this->isbn = $isbn;
  39.         self::$totalBooks++;
  40.     }
  41.  
  42.     // Implementation of getDetails method
  43.     public function getDetails() {
  44.         return "Title: {$this->title}, Author: {$this->author}, ISBN: {$this->isbn}, Type: " . self::getMediaType();
  45.     }
  46.  
  47.     // Implementation of borrow method
  48.     public function borrow($borrower) {
  49.         return "{$borrower} has borrowed the book: {$this->title}";
  50.     }
  51. }
  52.  
  53. // Class for Magazine, extending Media and implementing Borrowable
  54. class Magazine extends Media implements Borrowable {
  55.     private $issueNumber;
  56.     public static $totalMagazines = 0;
  57.     const MEDIA_TYPE = 'Magazine';
  58.  
  59.     // Constructor to initialize title, author, and issue number
  60.     public function __construct($title, $author, $issueNumber) {
  61.         parent::__construct($title, $author);
  62.         $this->issueNumber = $issueNumber;
  63.         self::$totalMagazines++;
  64.     }
  65.  
  66.     // Implementation of getDetails method
  67.     public function getDetails() {
  68.         return "Title: {$this->title}, Author: {$this->author}, Issue: {$this->issueNumber}, Type: " . self::getMediaType();
  69.     }
  70.  
  71.     // Implementation of borrow method
  72.     public function borrow($borrower) {
  73.         return "{$borrower} has borrowed the magazine: {$this->title}";
  74.     }
  75. }
  76.  
  77. // Usage example
  78. $libraryItems = [
  79.     new Book("1984", "George Orwell", "123456789"),
  80.     new Magazine("National Geographic", "Various", "2023-09")
  81. ];
  82.  
  83. // Loop through library items and display details
  84. foreach ($libraryItems as $item) {
  85.     echo $item->getDetails() . PHP_EOL;
  86.     echo $item->borrow("John Doe") . PHP_EOL . PHP_EOL; // Added extra newline for separation
  87. }
  88.  
  89. // Accessing static properties for total counts
  90. echo "Total Books: " . Book::$totalBooks . PHP_EOL;
  91. echo "Total Magazines: " . Magazine::$totalMagazines . PHP_EOL;
  92.  
  93. ?>
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement