Custom Search

OOPS in PHP 5 - 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
Explanation:
Here i am declaring BaseClass and DerivedClass but i am calling the the processClass with the Derived Class Object.

Custom Search

Related Post

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)