Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Abstract class for Media items
- abstract class Media {
- protected $title;
- protected $author;
- const MEDIA_TYPE = 'General';
- // Constructor to initialize title and author
- public function __construct($title, $author) {
- $this->title = $title;
- $this->author = $author;
- }
- // Abstract method to get details of the media
- abstract public function getDetails();
- // Static method to get media type
- public static function getMediaType() {
- return static::MEDIA_TYPE;
- }
- }
- // Interface for borrowable items
- interface Borrowable {
- public function borrow($borrower);
- }
- // Class for Book, extending Media and implementing Borrowable
- class Book extends Media implements Borrowable {
- private $isbn;
- public static $totalBooks = 0;
- const MEDIA_TYPE = 'Book';
- // Constructor to initialize title, author, and ISBN
- public function __construct($title, $author, $isbn) {
- parent::__construct($title, $author);
- $this->isbn = $isbn;
- self::$totalBooks++;
- }
- // Implementation of getDetails method
- public function getDetails() {
- return "Title: {$this->title}, Author: {$this->author}, ISBN: {$this->isbn}, Type: " . self::getMediaType();
- }
- // Implementation of borrow method
- public function borrow($borrower) {
- return "{$borrower} has borrowed the book: {$this->title}";
- }
- }
- // Class for Magazine, extending Media and implementing Borrowable
- class Magazine extends Media implements Borrowable {
- private $issueNumber;
- public static $totalMagazines = 0;
- const MEDIA_TYPE = 'Magazine';
- // Constructor to initialize title, author, and issue number
- public function __construct($title, $author, $issueNumber) {
- parent::__construct($title, $author);
- $this->issueNumber = $issueNumber;
- self::$totalMagazines++;
- }
- // Implementation of getDetails method
- public function getDetails() {
- return "Title: {$this->title}, Author: {$this->author}, Issue: {$this->issueNumber}, Type: " . self::getMediaType();
- }
- // Implementation of borrow method
- public function borrow($borrower) {
- return "{$borrower} has borrowed the magazine: {$this->title}";
- }
- }
- // Usage example
- $libraryItems = [
- new Book("1984", "George Orwell", "123456789"),
- new Magazine("National Geographic", "Various", "2023-09")
- ];
- // Loop through library items and display details
- foreach ($libraryItems as $item) {
- echo $item->getDetails() . PHP_EOL;
- echo $item->borrow("John Doe") . PHP_EOL . PHP_EOL; // Added extra newline for separation
- }
- // Accessing static properties for total counts
- echo "Total Books: " . Book::$totalBooks . PHP_EOL;
- echo "Total Magazines: " . Magazine::$totalMagazines . PHP_EOL;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement