Understanding OOP Polymorphism in PHP with example
Polymorphism in PHP Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface. The simple meaning of Polymorphism is ‘many forms’. Polymorphism allows us to use functions with same name but with different functionality. Here is an example code: class Animal { public $hungry = 'hell yeah.' ; function eat ( $food ) { $this -> hungry = 'not so much.' ; } } class Bird extends Animal { function eat ( $food ) { if ( $food == 'seed' ) { $this -> hungry = 'not so much.' ; } else { echo 'barf, I only like seed!' ; } } } As you can see, Bird isn’t all that different from Dog. They both eat food. However, because they are not the same type of Animal, they behave differently in response to the $food they are being fed. This is referred to as having their own implementations of the interface provided by Animal, or simpl...