Thursday, 9 February 2012

PHP 5 OOP Polymorphism Sample/Example

<?PHP
    /*
    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.

    */
    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);

?>

No comments:

Post a Comment