Polymorphism in PHP

Polymorphism is the combination of or invented from two Greek words. Poly means many and morph means forms, so you can easily say that polymorphism means many forms.

Polymorphism follows the concept of allowing a single name to be used for more than one related purpose. It has the ability to create a function, variable, or an object that has more than one form. It is also called overloading. Overloading is a way to performing multiple similar tasks with single or same object name. The advantage of overloading is that user has to remember single object name for different – different similar task.
Basically overloading is of 2 types

1) Function/ Method Overloading
2) Operator Overloading

Function Overloading:

When we write multiple function with same name but different- different data type or number of argument is called method / Function overloading . at the time of function calling the appropriate body of the function is called . use of multiple constructor in the Sme class is the best example of method overloading.
Eg. function fxn_sum(int, int) differs from fxn_sum(float, float). Therefore the method fxn_sum() has many forms depending on the parameters being passed to it.

 Example:-



class cls_fxnoverloading
{
public function __construct()
{
echo "I am default constructor";
}
public function __call($function_name , $args)
{
if($function_name == "overloded_fxn") //defining a function name with user call
{
$count = count($args);
switch($count)
{
case "1":
// overlodedFunction function has 1 argument
echo "user is passing one argument";
break;
case "2": //Incase of 2 args
echo "user is passing two argument";
break;
default:
echo "Function with arguments not exists";
}
}
else
{
throw new exception("Function $function_name does not exists ");
}
}
}
// default constructor
echo "
";
$obj = new cls_fxnoverloading();
// dynamically call overloading function with one argument
echo "
";
$obj->overloded_fxn("ankur");
// dynamically call overloading function with two argument
echo "
";
$obj->overloded_fxn("techflirt" , "ankur");
// exception case
echo "
";
$obj->overloded_fxn("techflirt" , "ankur", "ashu");
?>


Operator overloading:


Operator overloading is a technique in which we can perform multiple different- different similar task with single operator for example '+' is a operator which perform sum of 2 integer number, decimal number, and also in another language such as java & .net it concatenate two string . we can overload many operator for our class but we can overload only that symbols which one already working as operator in the language some special type of operator cannot be overloaded such as :: (scope resolution ) operator , ?: conditional operator , sizeof() , new, this etc.



Example Polymorphism:

class doer{
function do();
}
class do_b extends doer{
function do(){
print "I'm doing b";
}
}
class do_c extends doer{
function do(){
print "I'm doing c";
}
}
$sample_doer = get_a_sample_doer_somehow();
$sample_doer->do();


Polymorphism Hierarchy:-












polymorphism-hierarchy-freedownloadplugin


Thanks to www.freedownloadplugin.com

Comments

Popular posts from this blog

8 Best CMSs for building a well structured social networking website

​ PHP Web Programming and Business Benefits of PHP Development