Inheritance and Polymorphism in PHP: Mastering Object-Oriented Programming

Explore the concepts of inheritance and polymorphism in PHP, essential for creating flexible and reusable code. Learn how to extend classes, override methods, and achieve polymorphism for robust PHP applications.

2.3 Inheritance and Polymorphism

In the realm of object-oriented programming (OOP), inheritance and polymorphism are two fundamental concepts that allow developers to create flexible, reusable, and maintainable code. In PHP, these concepts are pivotal for designing robust applications that can adapt to changing requirements. This section will delve into the intricacies of inheritance and polymorphism, providing you with a comprehensive understanding of how to leverage these principles in your PHP projects.

Understanding Inheritance in PHP

Inheritance is a mechanism that allows a class to inherit properties and methods from another class. This promotes code reuse and establishes a natural hierarchy between classes. In PHP, inheritance is achieved using the extends keyword.

Basic Inheritance

Let’s start with a simple example to illustrate basic inheritance:

 1<?php
 2
 3// Base class
 4class Animal {
 5    public $name;
 6
 7    public function __construct($name) {
 8        $this->name = $name;
 9    }
10
11    public function speak() {
12        echo "The animal makes a sound.";
13    }
14}
15
16// Derived class
17class Dog extends Animal {
18    public function speak() {
19        echo "Woof! Woof!";
20    }
21}
22
23$dog = new Dog("Buddy");
24echo $dog->name; // Outputs: Buddy
25$dog->speak();   // Outputs: Woof! Woof!
26
27?>

In this example, the Dog class inherits from the Animal class. It overrides the speak method to provide a specific implementation for dogs.

Overriding Methods and Properties

When a derived class provides its own implementation of a method or property that exists in its parent class, it is known as overriding. This allows the derived class to modify or extend the behavior of the parent class.

 1<?php
 2
 3class Cat extends Animal {
 4    public function speak() {
 5        echo "Meow! Meow!";
 6    }
 7}
 8
 9$cat = new Cat("Whiskers");
10echo $cat->name; // Outputs: Whiskers
11$cat->speak();   // Outputs: Meow! Meow!
12
13?>

Here, the Cat class overrides the speak method to provide a cat-specific sound.

Access Control in Inheritance

PHP provides three levels of access control: public, protected, and private. These control the visibility of properties and methods in the context of inheritance.

  • Public: Accessible from anywhere.
  • Protected: Accessible within the class itself and by inheriting classes.
  • Private: Accessible only within the class itself.
 1<?php
 2
 3class Bird extends Animal {
 4    protected $wingSpan;
 5
 6    public function __construct($name, $wingSpan) {
 7        parent::__construct($name);
 8        $this->wingSpan = $wingSpan;
 9    }
10
11    public function fly() {
12        echo "{$this->name} is flying with a wingspan of {$this->wingSpan} meters.";
13    }
14}
15
16$bird = new Bird("Eagle", 2.5);
17$bird->fly(); // Outputs: Eagle is flying with a wingspan of 2.5 meters.
18
19?>

In this example, the wingSpan property is protected, meaning it can be accessed by the Bird class and any class that extends Bird.

Achieving Polymorphism in PHP

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It is a key concept in OOP that enables flexibility and integration of different classes.

Method Overloading and Overriding

PHP does not support method overloading (having multiple methods with the same name but different parameters) directly. However, method overriding is a form of polymorphism where a subclass provides a specific implementation of a method that is already defined in its superclass.

Interfaces and Polymorphism

Interfaces in PHP define a contract that classes must adhere to. They are a powerful tool for achieving polymorphism.

 1<?php
 2
 3interface Sound {
 4    public function makeSound();
 5}
 6
 7class Lion implements Sound {
 8    public function makeSound() {
 9        echo "Roar!";
10    }
11}
12
13class Elephant implements Sound {
14    public function makeSound() {
15        echo "Trumpet!";
16    }
17}
18
19function animalSound(Sound $animal) {
20    $animal->makeSound();
21}
22
23$lion = new Lion();
24$elephant = new Elephant();
25
26animalSound($lion);      // Outputs: Roar!
27animalSound($elephant);  // Outputs: Trumpet!
28
29?>

In this example, both Lion and Elephant implement the Sound interface, allowing them to be used interchangeably in the animalSound function.

Abstract Classes and Polymorphism

Abstract classes provide a way to define common behavior that can be shared among subclasses. They can contain abstract methods that must be implemented by derived classes.

 1<?php
 2
 3abstract class Vehicle {
 4    abstract public function startEngine();
 5
 6    public function stopEngine() {
 7        echo "Engine stopped.";
 8    }
 9}
10
11class Car extends Vehicle {
12    public function startEngine() {
13        echo "Car engine started.";
14    }
15}
16
17class Motorcycle extends Vehicle {
18    public function startEngine() {
19        echo "Motorcycle engine started.";
20    }
21}
22
23$car = new Car();
24$motorcycle = new Motorcycle();
25
26$car->startEngine();      // Outputs: Car engine started.
27$motorcycle->startEngine(); // Outputs: Motorcycle engine started.
28
29?>

Here, Car and Motorcycle are required to implement the startEngine method defined in the Vehicle abstract class.

Visualizing Inheritance and Polymorphism

To better understand the relationships between classes and interfaces, let’s visualize these concepts using a class diagram.

    classDiagram
	    class Animal {
	        +String name
	        +speak()
	    }
	
	    class Dog {
	        +speak()
	    }
	
	    class Cat {
	        +speak()
	    }
	
	    Animal <|-- Dog
	    Animal <|-- Cat
	
	    class Sound {
	        <<interface>>
	        +makeSound()
	    }
	
	    class Lion {
	        +makeSound()
	    }
	
	    class Elephant {
	        +makeSound()
	    }
	
	    Sound <|.. Lion
	    Sound <|.. Elephant

This diagram illustrates how Dog and Cat inherit from Animal, and how Lion and Elephant implement the Sound interface.

PHP Unique Features in Inheritance and Polymorphism

PHP offers several unique features that enhance inheritance and polymorphism:

  • Traits: PHP allows the use of traits, which are a mechanism for code reuse in single inheritance languages like PHP. Traits can be used to include methods in a class, providing a way to share functionality between classes.
 1<?php
 2
 3trait Logger {
 4    public function log($message) {
 5        echo "Log: $message";
 6    }
 7}
 8
 9class Application {
10    use Logger;
11}
12
13$app = new Application();
14$app->log("Application started."); // Outputs: Log: Application started.
15
16?>
  • Late Static Binding: This feature allows you to reference the called class in a context of static inheritance.
 1<?php
 2
 3class Base {
 4    public static function who() {
 5        echo __CLASS__;
 6    }
 7
 8    public static function test() {
 9        static::who(); // Here comes Late Static Bindings
10    }
11}
12
13class Child extends Base {
14    public static function who() {
15        echo __CLASS__;
16    }
17}
18
19Child::test(); // Outputs: Child
20
21?>

Differences and Similarities with Other Languages

While PHP shares many OOP concepts with languages like Java and C++, there are differences:

  • No Method Overloading: Unlike Java, PHP does not support method overloading.
  • Dynamic Typing: PHP is dynamically typed, which affects how polymorphism is implemented compared to statically typed languages.
  • Traits: PHP’s trait system is unique and not found in languages like Java.

Design Considerations

When using inheritance and polymorphism in PHP, consider the following:

  • Avoid Deep Inheritance Hierarchies: Deep hierarchies can make code difficult to understand and maintain.
  • Use Interfaces for Flexibility: Interfaces provide a flexible way to implement polymorphism without the constraints of inheritance.
  • Leverage Traits for Code Reuse: Use traits to share functionality between classes without creating complex inheritance structures.

Try It Yourself

Experiment with the code examples provided. Try creating your own classes and interfaces to see how inheritance and polymorphism can simplify your code design. Modify the examples to add new methods or properties, and observe how PHP handles these changes.

Knowledge Check

  • Explain how inheritance promotes code reuse.
  • Describe how polymorphism enhances flexibility in code design.
  • What are the differences between abstract classes and interfaces in PHP?

Embrace the Journey

Remember, mastering inheritance and polymorphism is a journey. As you continue to explore these concepts, you’ll find new ways to apply them in your projects. Keep experimenting, stay curious, and enjoy the process of learning and growing as a PHP developer.

Quiz: Inheritance and Polymorphism

Loading quiz…
Revised on Thursday, April 23, 2026