Posts

Showing posts with the label Polymorphism

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...

Polymorphism in PHP

Image
Polymorphism is the combination of or invented from two Greek words. Poly means many and morph means forms, so you can easily say that polymorphism means many forms. Polymorphism follows the concept of allowing a single name to be used for more than one related purpose. It has the ability to create a function, variable, or an object that has more than one form. It is also called overloading. Overloading is a way to performing multiple similar tasks with single or same object name. The advantage of overloading is that user has to remember single object name for different – different similar task. Basically overloading is of 2 types 1) Function/ Method Overloading 2) Operator Overloading Function Overloading: When we write multiple function with same name but different- different data type or number of argument is called method / Function overloading . at the time of function calling the appropriate body of the function is called . use of multiple constructor in th...