Introduction and Lesson Goal

Today's mission involves using multiple Object-Oriented Programming (OOP) principles to tackle complex tasks. When principles like Encapsulation, Abstraction, Polymorphism, and Composition are blended, the resulting code becomes streamlined and easier to manage.

Our goal is to dissect two real-world examples, gaining insights into how these principles can seamlessly orchestrate solutions.

Real-life Example 1: Building an Online Library System

Let's design an online library system, as we aim to reinforce our understanding of Encapsulation and Polymorphism. Encapsulation will help us protect the attributes of books, members, and transactions, ensuring they are accessible in a controlled manner. Polymorphism will demonstrate its power by enabling a single interface to represent different underlying forms, such as digital and print versions of books.

<?php

// Base class for different types of library users
class Member {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function checkOutBook($book) {
        echo "{$this->name} checked out {$book->getBookType()} book {$book->getTitle()}.\n";
    }
}

// Base class for different types of books
abstract class Book {
    private $title;

    public function __construct($title) {
        $this->title = $title;
    }

    public function getTitle() {
        return $this->title;
    }

    public abstract function getBookType();
}

// Inherits from Book, represents a digital book
class DigitalBook extends Book {
    public function getBookType() {
        return "Digital";
    }
}

// Inherits from Book, represents a physical book
class PhysicalBook extends Book {
    public function getBookType() {
        return "Physical";
    }
}

// Library class that manages members and books
class Library {
    private $members = [];
    private $books = [];

    public function addMember($member) {
        $this->members[] = $member;
    }

    public function addBook($book) {
        $this->books[] = $book;
    }
}

$myLibrary = new Library();

$alice = new Member("Alice");
$bob = new Member("Bob");

$myLibrary->addMember($alice);
$myLibrary->addMember($bob);

$digitalBook = new DigitalBook("The PHP Handbook");
$physicalBook = new PhysicalBook("Learning PHP Design Patterns");

$myLibrary->addBook($digitalBook);
$myLibrary->addBook($physicalBook);

$alice->checkOutBook($digitalBook);  // Outputs: Alice checked out Digital book The PHP Handbook.
$bob->checkOutBook($physicalBook);   // Outputs: Bob checked out Physical book Learning PHP Design Patterns.

?>

In this code snippet, Encapsulation is observed clearly through the class structures and the controlled access to their properties. Polymorphism is vividly illustrated by how both DigitalBook and PhysicalBook classes inherit from the Book class but provide their own implementations of the getBookType method. This setup allows objects of DigitalBook and PhysicalBook to be used interchangeably when a book’s type needs to be identified, demonstrating Polymorphism's capability to work with objects of different classes through a common interface.

  • Encapsulation ensures that details about members and books are well-contained within their respective classes.
  • Polymorphism showcases flexibility by treating different book types uniformly, making the system more adaptive and scalable.
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal