Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface.
– Polymorphism is basically derived from the Greek which means 'many forms'.
– In other words, polymorphism is what join bunch of classes with one interface.
polymorphism is a key part of php oops.
– As you can see in above image, printer class has a print method which performs multi task, such as print to screen and print to paper.
– Also, you can take example of twin brothers, they look like same but have different characters.
Here is a example which tell you about polymorphism:
class ParentClass
{
public function myOwnMethod()
{
echo "ParentClass method called";
}
}
class ChildClass extends ParentClass
{
public function myOwnMethod()
{
echo "ChildClass method called";
}
}
function runClass(ParentClass $c)
{
$c->myOwnMethod();
}
$c = new ChildClass();
runClass($c);
All Comments