What is Polymorphism?
Polymorphism in PHP5 is a technique where the function to be called is detected based on the class object calling it at runtime. The basis of Polymorphism is Inheritance and function overridden.
Example – Basic Polymorphism
class BaseClass
{
public function myMethod()
{
echo "BaseClass method called";
}
}
class DerivedClass extends BaseClass
{
public function myMethod()
{
echo "DerivedClass method called";
}
}
function processClass(BaseClass $c)
{
$c->myMethod();
}
$c = new DerivedClass();
processClass($c);
Output:
DerivedClass method called
Here i am declaring BaseClass and DerivedClass but i am calling the the processClass with the Derived Class Object.
Tuesday, April 20, 2010
PHP OOPS - Polymorphism
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment