Home > PHP, PHP Class Examples, PHP5 OOPS Tutorials > PHP5 OOPS Tutorials – Polymorphism

PHP5 OOPS Tutorials – Polymorphism

February 11th, 2008 admin Leave a comment Go to comments

In this tutorial we will learn the following:

  • Meaning of Polymorphism
  • Why method polymorphism cannot be achieved in PHP
  • PHP 5 Object Polymorphism

Meaning of Polymorphism

Polymorphism is derived from two Greek words. Poly (meaning many) and morph (meaning forms). Polymorphism means many forms. In C you have two methods with the same name that have different function signatures and hence by passing the correct function signature you can invoke the correct method.

This is how polymorphism is achieved in languages like C where in a function sum(int, int) differs from sum(float, float). Therefore the method sum() has many forms depending on the parameters being passed to it.

The meaning with Object Oriented languages changes. With Object Oriented language polymorphism happens:

When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism




Why method polymorphism cannot be achieved

The reason why polymorphism for methods is not possible in PHP is because you can have a method that accepts two parameters and call it by passing three parameters. This is because PHP is not strict and contains methods like func_num_args() and func_get_arg() to find the number of arguments passed and get a particular parameter.

Because PHP is not type strict and allows variable arguments, this is why method polymorphism is not possible.

PHP 5 Polymorphism

Since PHP 5 introduces the concept of Type Hinting, polymorphism is possible with class methods. The basis of polymorphism is Inheritance and overridden methods.

Lets look at an example:

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

In the above example, object $c of class DerievedClass is executed and passed to the processClass() method. The parameter accepted in processClass() is that of BassClass. Within the processClass() the method myMethod() is being called. Since the method is being called on the class variable of BaseClass, it would not be wrong to assume that myMethod() of class BaseClass will be called. But, as per the definition “When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism”, myMethod() will be called on object DerievedClass. The reason why this happens is because the object of DerievedClass is being passed and hence the method myMethod() of DerievedClass will be called.

Please feel free to write back for any clarification.

Subscribe to my newsletter:

Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)

  1. PHP5 Tutorial – Learn to create a PHP5 Class
  2. PHP5 Tutorial – Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial – Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial – Defining Methods of a PHP5 Class
  5. PHP5 Tutorial – Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
  8. PHP5 Tutorial – $this variable explained
  9. PHP5 Tutorial – instanceOf Operator Explained
  10. PHP5 Tutorial – Defining Class Constants
  11. PHP5 Tutorial – Inheritance
  12. PHP5 Tutorials – Abstract Class and Interface
  13. PHP5 Tutorial – Magic Methods – __toString() method
  14. PHP5 Tutorial – Magic Methods – __get() and __set()
  15. PHP5 Tutorial – Magic Methods – __isset() and __unset()
  16. PHP5 Tutorial – Magic Methods – __call() method
  17. PHP5 Tutorial – Magic Methods – __autoload() method
  18. PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
  19. PHP5 Tutorial – Magic Methods – __clone() method





Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. June 3rd, 2008 at 07:05 | #1

    Hello,
    Whatever explanation you provided is awesome but example is wrong. Above example is relevant to Overriding …

    class Sample
    {
    public function createLine()
    {
    echo “########################”;
    }
    public function createLine($chr)
    {
    for ($g=0;$g<10;$g++)
    echo $chr;
    }
    public function createLine($str,$intLength)
    {
    for($g=0;$g<$intLength;$g++)
    echo $chr
    }
    }

    In above example createLine is one of example of polymorphism.

    Thanks
    -Dipsolution

  2. spats
    August 5th, 2008 at 08:39 | #2

    Changed processClass() as below,

    function processClass(BaseClass $c) {
    if($c instanceof BaseClass)
    print(“Its Base class \n”);
    else
    print(” c \n”);

    $c->myMethod();
    }

    As per your explanation it should print “Its Base class” but script output is
    $ php test.php
    Its Base class
    DerivedClass method called

    Strange object is still instance of base class but derived object’s method is called……. any explanation?

  3. spats
    August 5th, 2008 at 08:44 | #3

    Ok, got the answer
    Since DerivedClass derived from BaseClass, any object of DerivedClass is also aninstance of BaseClass.

    Slight change in above function explains it
    function processClass(BaseClass $c) {
    if($c instanceof DerivedClass)
    print(“Its derived class \n”);
    else
    print(” Its base class \n”);

    $c->myMethod();
    }

    $ php test.php
    Its derived class
    DerivedClass method called

  4. spats
    August 5th, 2008 at 08:51 | #4

    Mandrik Smith,
    your example is wrong. were u able to run ur script? it gives error
    Fatal error: Cannot redeclare Sample::createLine() in /home/sudhirp/test.php on line 8

    Whatever author explans about polymorphism in php is correct.

  5. George
    October 9th, 2008 at 12:45 | #5

    Its really great to learn from you.its good for beginners..

    thanks a lot..

  6. admin
    November 15th, 2008 at 19:37 | #6

    Hi Sudhir,

    Not able to get you… my example does not have a method createLine()?

    Let me know if you need help with it.

    Regards,
    Suniil

  7. paul
    July 17th, 2009 at 00:12 | #7

    thanks guys very good for peolpe like us who are beginners

  8. dhinessh bupathy
    February 8th, 2010 at 18:52 | #8

    hi sunil,
    You are doing a great job regarding php.
    I referred ur post about oops concept.
    how many types of Polymorphism is possible in php 5?

  9. admin
    February 12th, 2010 at 09:12 | #9

    Hi Dhinessh,

    Thank you for your kind words.

    I understand your question with respect to early and late binding. If this was your question, then I must say that in PHP5, Polymorphism only works in methods (to my experience)… and syntax like below will not work

    Parent p = new Child();

    Hope this answers your question.

    Regards,
    Sunil

  10. nithin
    February 12th, 2010 at 20:07 | #10

    ! great job regarding php.

  1. February 11th, 2008 at 13:35 | #1
Enter this code to leave comment (Sorry, but bots get me crazy :) )