Current Location: Home> Latest Articles> In-depth PHP OOP Guide: Implementing Inheritance and Polymorphism

In-depth PHP OOP Guide: Implementing Inheritance and Polymorphism

M66 2025-07-21

Understanding Inheritance in PHP

Inheritance is a key mechanism in object-oriented programming that allows a class to be based on another class. Subclasses inherit properties and methods from their parent classes, enabling code reuse and extension. In PHP, the extends keyword is used to create subclasses, and parent:: is used to call parent class methods or properties.

Example of Inheritance

class Animal {
   protected $name;
   protected $age;

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

   public function getInfo() {
      return "Name: " . $this->name . ", Age: " . $this->age;
   }
}

class Dog extends Animal {
   public function bark() {
      return "Woof!";
   }
}

$dog = new Dog("Rex", 3);
echo $dog->getInfo();  // Outputs "Name: Rex, Age: 3"
echo $dog->bark();     // Outputs "Woof!"

The code above shows a base class Animal with properties name and age and a method to get info. The subclass Dog inherits these and adds a bark() method, demonstrating basic inheritance.

Implementing Polymorphism in PHP

Polymorphism means that the same method can behave differently on different objects. In PHP, this is mainly achieved through interfaces (interface) and abstract classes (abstract class). Interfaces define method signatures, while abstract classes provide partial implementation. Subclasses must implement abstract methods, making the code flexible and extensible.

Polymorphism Example: Interfaces and Implementing Classes

interface Shape {
   public function area();
   public function perimeter();
}

class Rectangle implements Shape {
   private $length;
   private $width;

   public function __construct($length, $width) {
      $this->length = $length;
      $this->width = $width;
   }

   public function area() {
      return $this->length * $this->width;
   }

   public function perimeter() {
      return 2 * ($this->length + $this->width);
   }
}

class Circle implements Shape {
   private $radius;

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

   public function area() {
      return pi() * pow($this->radius, 2);
   }

   public function perimeter() {
      return 2 * pi() * $this->radius;
   }
}

$rectangle = new Rectangle(5, 3);
$circle = new Circle(2);

echo $rectangle->area();     // Outputs 15
echo $rectangle->perimeter();  // Outputs 16

echo $circle->area();        // Outputs 12.566370614359
echo $circle->perimeter();   // Outputs 12.566370614359

In this example, Rectangle and Circle both implement the Shape interface and must define area() and perimeter() methods. Different implementations allow the same method to behave differently, illustrating polymorphism.

Summary

Inheritance and polymorphism are foundational concepts in PHP object-oriented programming. Mastering them improves code organization and reusability. Inheritance allows subclasses to inherit properties and methods, reducing code duplication. Polymorphism, through interfaces and abstract classes, enables the same method to behave differently depending on the object, increasing flexibility. The examples provided help beginners grasp these concepts and write higher-quality PHP code.