Introduction to Polymorphism in PHP

Introduction to Polymorphism

Welcome back! We're continuing our journey into object-oriented programming (OOP) with a new and exciting topic: Polymorphism. You've already learned about classes, objects, and inheritance, which are essential building blocks of OOP. Now, it's time to explore how polymorphism can make your code more flexible and reusable.

What You'll Learn

Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. This provides a way to use a single interface to represent different types of objects. Here’s what we’ll cover in this lesson:

  1. Understanding Polymorphism: We'll discuss what polymorphism is and why it's a powerful concept in programming.
  2. Implementing Polymorphism in PHP: You'll learn how to use method overriding and interfaces to achieve polymorphism.

Understanding Polymorphism

Polymorphism in PHP allows you to call derived class methods through a parent class reference. This can make your code more dynamic and generic. For example, consider the following code snippet:

PHP
<?php

// Define a base class Person with name and age attributes
class Person {
    protected $name;
    protected $age;

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

    // Define a method to display details, meant to be overridden in derived classes
    public function display() {
        echo "Name: {$this->name}, Age: {$this->age}\n";
    }
}

// Define a derived class Student with a major attribute
class Student extends Person {
    private $major;

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

    // Override the display method to include the major attribute
    public function display() {
        parent::display();
        echo "Major: {$this->major}\n";
    }
}

// Define a derived class Teacher with a subject attribute
class Teacher extends Person {
    private $subject;

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

    // Override the display method to include the subject attribute
    public function display() {
        parent::display();
        echo "Subject: {$this->subject}\n";
    }
}

// Create instances of Student and Teacher using parent class references
$person1 = new Student("Alice", 30, "Computer Science");
$person2 = new Teacher("Bob", 25, "Mathematics");

// Call the display method on the parent class references. The appropriate derived class method will be invoked.
$person1->display();
$person2->display();

?>

In this example, the display method of the derived classes (Student and Teacher) overrides the method in Person. When we call display on a parent class reference, the appropriate derived class method is invoked.

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