Lesson 4
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
1<?php 2 3// Define a base class Person with name and age attributes 4class Person { 5 protected $name; 6 protected $age; 7 8 public function __construct($name, $age) { 9 $this->name = $name; 10 $this->age = $age; 11 } 12 13 // Define a method to display details, meant to be overridden in derived classes 14 public function display() { 15 echo "Name: {$this->name}, Age: {$this->age}\n"; 16 } 17} 18 19// Define a derived class Student with a major attribute 20class Student extends Person { 21 private $major; 22 23 public function __construct($name, $age, $major) { 24 parent::__construct($name, $age); 25 $this->major = $major; 26 } 27 28 // Override the display method to include the major attribute 29 public function display() { 30 parent::display(); 31 echo "Major: {$this->major}\n"; 32 } 33} 34 35// Define a derived class Teacher with a subject attribute 36class Teacher extends Person { 37 private $subject; 38 39 public function __construct($name, $age, $subject) { 40 parent::__construct($name, $age); 41 $this->subject = $subject; 42 } 43 44 // Override the display method to include the subject attribute 45 public function display() { 46 parent::display(); 47 echo "Subject: {$this->subject}\n"; 48 } 49} 50 51// Create instances of Student and Teacher using parent class references 52$person1 = new Student("Alice", 30, "Computer Science"); 53$person2 = new Teacher("Bob", 25, "Mathematics"); 54 55// Call the display method on the parent class references. The appropriate derived class method will be invoked. 56$person1->display(); 57$person2->display(); 58 59?>

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.

Why It Matters

Polymorphism is crucial because it introduces flexibility and scalability to your code:

  • Code Flexibility: Polymorphism allows you to write functions that can operate on objects of different types through a common interface.
  • Reusability: You can extend and reuse your code more efficiently by leveraging polymorphism.
  • Simplified Code Management: Polymorphism helps you manage and understand your code better, as similar operations are handled in a unified manner.

Are you excited to dive into the practice section and apply polymorphism to your programs? Let's get started and see the power of this concept in action!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.