PHP5 Tutorial – Polymorphism

January 1st, 2011 by laeeq | 1 comment

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.

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.

 

 

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Find Time Between Two Dates in PHP
  • Creating Objects in PHP5 Class
  • PHP5 Tutorial – Definition of a class attribute
  • PHP5 Tutorial – Creating a PHP5 Destructor
  • PHP5 Tutorial- Definition of Constructor
  • PHP5 Tutorial – __clone() method
  • PHP5 Tutorial – magic method
  • PHP5 Tutorial – Magic Methods – __autoload() method
  • PHP5 Tutorial – magic method __call()
  • PHP5 Tutorial – Magic Methods – __isset() and __unset()
  • PHP5 Tutorial – get()) and __set() magic method
  • PHP5 Tutorial – __toString() method
  • PHP5 Tutorial – abstract class and interface
  • PHP5 Tutorial- Inheritance
  • PHP5 Tutorial – exception handling
  • PHP5 Tutorial – Final Class and Final Method
  • PHP5 Tutorial – Static Data Members and Methods
  • PHP5 Tutorial – Defining Class Constants
  • PHP5 Tutorial – instanceOf Operator
  • PHP5 Tutorial – $this variable
  •  

     

    1. September 22nd, 2012 at 05:25 | #1

      Very soon this site will be famous among all blogging users, due
      to it’s pleasant articles

    1. No trackbacks yet.