Polymorphism in OOP
Polymorphism means many shapes or many forms. Polymorphism is achieved through the inheritance of a PHP abstract class, as well as by implementing PHP interfaces.
Polymorphism helps you create a format or a standard for solving a specific problem programmatically, instead of just focusing on a single implementation of a solution.
How do we apply this in PHP and what benefit do we get in using this feature? Let’s take the example codes in the following subsections as an example, starting with a PHP abstract class.
Polymorphism with a PHP abstract class
When using abstract classes in PHP, we can implement polymorphism by using abstract functions. The following example is of a PHP abstract class:
AbstractAnimal.php
<?php namespace Animals\Polymorphism\AbstractExample; abstract class AbstractAnimal { abstract public function makeSound(); }
Every PHP abstract class ideally should start with the Abstract
prefix,...