PHP Polymorphism

Created with Sketch.

PHP Polymorphism

Summary: in this tutorial, you will learn about the polymorphism concept in object-oriented programming. And you’ll also learn how to implement polymorphism in PHP using abstract classes or interfaces.

What is polymorphism?

Polymorphism is a Greek word that literally means many forms. In object-oriented programming, polymorphism is closely related to inheritance.

Polymorphism allows objects of different classes to respond differently based on the same message.

To implement polymorphism in PHP, you can use either abstract classes or interfaces.

Polymorphism helps you create a generic framework that takes the different object types that share the same interface.

Later, when you add a new object type to the system, you don’t need to change the framework to accommodate the new object type as long as it implements the same interface.

By using polymorphism, you can reduce coupling and increase code reusability.

PHP polymorphism using an abstract class

First, define an abstract class named Person that has an abstract method called greet().

<?php

abstract class Person
{
abstract public function greet();
}

Code language: HTML, XML (xml)

Second, define the English, German, and French classes that extend the Person class. The greet() method of each class returns a different greeting message.

<?php
// ...
class English extends Person
{
public function greet()
{
return 'Hello!';
}
}

class German extends Person
{
public function greet()
{
return 'Hallo!';
}
}

class French extends Person
{
public function greet()
{
return 'Bonjour!';
}
}

Code language: HTML, XML (xml)

Third, define a function called greeting() that accepts an array of Person objects and calls the greet() method of each object:

<?php
//...
function greeting($people)
{
foreach ($people as $person) {
echo $person->greet() . '<br>';
}
}

Code language: HTML, XML (xml)

Fourth, define an array of objects of the English, German, and French classes and pass it to the greeting() function:

<?php
//...
$people = [
new English(),
new German(),
new French()
];

greeting($people);

Code language: HTML, XML (xml)

Output:

Hello!
Hallo!
Bonjour!

 

Due to the specific implementation of the greet() method in each class, these objects return different greeting messages.

The following class diagram illustrates the relationships between the classes:

Later, if you want to create a new class that extends the Person class, you don’t need to change the greeting() function.

For example, you can define a new class called American that extends the Person class:

<?php
// ...
class American extends Person
{
public function greet()
{
return 'Hi!';
}
}

Code language: HTML, XML (xml)

And use the object of the American class in the greeting() function as follows:


<?php
// ...
$people = [
new English(),
new German(),
new French(),
new American()
];

greeting($people);

Code language: HTML, XML (xml)

Output:

Hello!
Hallo!
Bonjour!
Hi!

 

PHP polymorphism using an interface

The following example is the same as the above except that it uses an interface instead of an abstract class.

First, define an interface called Greeting that has the greet() method. The greet() method returns a greeting message:

<?php

interface Greeting
{
public function greet();
}

Code language: HTML, XML (xml)

Second, define the English, German, and French classes that implement the Greeting interface:


<?php
// ...
class English implements Greeting
{
public function greet()
{
return 'Hello!';
}
}

class German implements Greeting
{
public function greet()
{
return 'Hallo!';
}
}

class French implements Greeting
{
public function greet()
{
return 'Bonjour!';
}
}

Code language: HTML, XML (xml)

Third, define the greeting() function that accepts an array of objects that implements the Greeting interface:

<?php
// ...
function greeting($greeters)
{
foreach ($greeters as $greeter) {
echo $greeter->greet() . '<br>';
}
}

Code language: HTML, XML (xml)

Finally, define an array of the Greeting objects and pass it to the greeting() function:


<?php
// ...
$greeters = [
new English(),
new German(),
new French(),
];

greeting($greeters);

Code language: HTML, XML (xml)

Output:

Hello!
Hallo!
Bonjour!

 

The following class diagram shows the relationships between the interface and classes:

Summary

  • Polymorphism allows objects of different classes to respond differently to the same message.
  • Use interfaces or abstract classes to implement polymorphism in PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *