Building Smarter and More Flexible Code

In Part 2, you learned the basics of OOP: Classes, Objects, Properties, and Methods. Now, we'll explore the concepts that make OOP truly powerful for building large, maintainable applications: Inheritance, Abstract Classes, and Interfaces.


1. Inheritance

Inheritance allows one class (the "child" class) to inherit all the public properties and methods from another class (the "parent" class). This is a powerful tool for code reuse.

Analogy: Think of a general Animal class and a specific Dog class. A dog is an animal, so it shares common properties (like having a name) and methods (like being able to eat). The Dog class can "extend" the Animal class to inherit these features, and then add its own specific ones, like a bark() method.

<?php
    // Parent Class
    class Animal {
        public $name;

        public function eat() {
            return $this->name . " is eating.";
        }
    }

    // Child Class inherits from Animal
    class Dog extends Animal {
        public function bark() {
            return "Woof!";
        }
    }

    $myDog = new Dog();
    $myDog->name = "Buddy";
    
    echo $myDog->eat();   // Outputs: Buddy is eating. (Method from parent)
    echo "<br>";
    echo $myDog->bark(); // Outputs: Woof! (Method from child)
?>

2. Abstract Classes

An abstract class is a special kind of class that cannot be used to create an object directly. It's a template that forces child classes to implement certain methods.

Analogy: An abstract class is like a partially completed blueprint. It might define that every house must have a get_address() method, but it doesn't say how to get the address. Each specific type of house (like Apartment or Villa) must provide its own implementation of that method.

<?php
    abstract class Shape {
        // A regular method that all children will inherit
        public function getName() {
            return "I am a shape.";
        }
        
        // An abstract method that children MUST implement
        abstract public function calculateArea();
    }

    class Circle extends Shape {
        private $radius;
        public function __construct($radius) { $this->radius = $radius; }
        
        // Implementing the abstract method
        public function calculateArea() {
            return pi() * $this->radius * $this->radius;
        }
    }

    $myCircle = new Circle(10);
    echo $myCircle->calculateArea();
?>

3. Interfaces

An interface is like a contract. It defines a set of methods that a class must implement. Unlike an abstract class, an interface has no implementation at all—only method names.

Analogy: An interface is like a USB standard. It doesn't care if you're a keyboard, a mouse, or a flash drive (different classes), but it guarantees that if you want to be "USB compatible" (implement the interface), you must have a specific type of plug (method).

<?php
    // The contract
    interface Loggable {
        public function logMessage();
    }

    class User implements Loggable {
        public function logMessage() {
            return "User activity logged.";
        }
    }
    
    class Order implements Loggable {
        public function logMessage() {
            return "Order #123 logged.";
        }
    }
?>

These advanced concepts are the bedrock of modern PHP frameworks like Laravel and Symfony. Understanding them is essential for writing professional, scalable, and maintainable applications.