0% found this document useful (0 votes)
346 views

Overloading and Overriding in PHP March 24

This document discusses method overloading and overriding in PHP. It begins by explaining that overloading allows a method to take on extra work depending on arguments passed, while overriding replaces a parent method in a child class. It then provides examples of implementing overloading using the __call magic method and overriding by redeclaring a method in a child class. The document concludes that overloading and overriding provide flexibility in PHP classes by allowing methods to change behavior based on arguments or inheritance.

Uploaded by

Santosh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
346 views

Overloading and Overriding in PHP March 24

This document discusses method overloading and overriding in PHP. It begins by explaining that overloading allows a method to take on extra work depending on arguments passed, while overriding replaces a parent method in a child class. It then provides examples of implementing overloading using the __call magic method and overriding by redeclaring a method in a child class. The document concludes that overloading and overriding provide flexibility in PHP classes by allowing methods to change behavior based on arguments or inheritance.

Uploaded by

Santosh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Overloading and Overriding in PHP March 24, 2013 33 Comments

Function or method Overloading and overriding method is very basic and useful feature of any oop language.
In this tutorial we will discuss implementation of method overloading and overriding in php. Here first we
will explore basics of overloading and overriding. After exploration of basics we will implement overloading
and overriding in php. Before going further I am assuming that you have basic knowledge of classes and
inheritance in php. Also you have understanding about magic method in php. Magic method because
overloading in php can be implmented using magic methods.

What is Method Overriding in OOP ?


Basic meaning of overriding in oop is same as real word meaning. In real word meaning
of overriding phenomena of replacing the same parental behavior in child. This is same in case of method
overriding in oop. In oop meaning of overriding is to replace parent class method in child class. Or in simple
technical word method overriding mean changing behavior of the method. In oop overriding is process by
which you can re-declare your parent class method in child class. So basic meaning of overriding in oop is to
change behavior of your parent class method.

Normally method overriding required when your parent class have some method, but in your child class you
want the same method with different behavior. By overriding of method you can complete change its
behavior from parent class. To implment method overiding in oop we commonly create same method in child
class.

What is Method Overloading in OOP ?


Overloading in oop is same as overloading in real word. In real word overloading means assigning extra
work to same machine or person. In oop method overloading is same. By process of method overloading you
are asking your method to some extra work. Or in some cases we can say some different work also.

Normally method overloading in oop is managed on the basis of the argument passed in function. We can
achieve overloading in oop by providing different argument in same function.

Overloading and Overriding in PHP


Hope your basic concept of overloading and overriding is clear now. Now let us explore implementation of
overloading and overriding in php.

Implementation of overriding in php is very easy. If your parent class has a function. You can create function
with same name in your child class to override the function. Implementation of overriding can not be
achieved by creating 2 function with same name and different argument in php. Because we can not create
same name function more than 1 time in php class. To implement overloading we need to take help of magic
method in php. In below section we will explore overloading and overriding one by one.

Overloading in PHP
As we know that we can not implement overloading by create 2 function in with same name in class. So
to implement overloading in php we will take help of magic method __call. Magic method __call invoked
when method called by class object is not available in class. So here we will not create method exactly and
will take help of __call method. Now call method will provide us 2 argument, 1st name of the method called
and parameter of the function. Now with the help of either switch , case or if else we
will implement overloading in php. Following is very simple example of overloading in php.

class test
{
public function __construct()
{
//Your logic for constructor
}
public function __call($method_name , $parameter)
{
if($method_name == "overlodedFunction") //Function overloading logic for function name
overlodedFunction
{
$count = count($parameter);
switch($count)
{
case "1":
//Business log in case of overlodedFunction function has 1 argument
echo "You are passing 1 argument";
break;
case "2": //Incase of 2 parameter
echo "You are passing 2 parameter";
break;
default:
throw new exception("Bad argument");
}
}
else
{
throw new exception("Function $method_name does not exists ");
}
}
}
$a = new test();
$a->overlodedFunction("ankur");
$a->overlodedFunction("techflirt" , "ankur");

As in above class test magic method __call is implemented which is managing overloading

public function __call($method_name , $parameter)


{
if($method_name == "overlodedFunction") //Function overloading logic for function name
overlodedFunction
{
$count = count($parameter);
switch($count)
{
case "1":
//Business log in case of overlodedFunction function has 1 argument
echo "You are passing 1 argument";
break;
case "2": //Incase of 2 parameter
echo "You are passing 2 parameter";
break;
default:
throw new exception("Bad argument");
}
}
else
{
throw new exception("Function $method_name does not exists ");
}
}
As we know that __call magic method invoked when method is not available in the class. So in case of above
test class example we have not created function overlodedFunction. So whenever method overlodedFunction
is called __call invoked. __call pass 2 variable, first name of the called method and other is parameter
passed in the called function.

Now in the __call function I have applied if condition to ensure that our business logic of overloading works
only for overlodedFunction function. In if block we have counted number of argument in parameter and
applied business logic.

Overriding in PHP
Overriding in php is very easy. As we know that overriding is process of modifying the inherited method. So
in case of inheritance you only need to create method with same name in your child class which you want to
override. Following is example of overriding of method in php.

class testParent
{
public function f1()
{
echo 1;
}
public function f2()
{
echo 2;
}
}
class testChild
{
function f2($a) //overriding function f2
{
echo "$a";
}
}
$a = new testChild();
$a->f2("ankur");//it will print ankur

In above example you are overriding function f2. While overriding you are free to change business
logic, visibility and number of parameter.

I hope your concept of the overloading and overriding in php is clear now.

For more detail about overloading and overriding in php you can read:
http://php.net/manual/en/language.oop5.overloading.php
http://en.wikipedia.org/wiki/Function_overloading
http://en.wikipedia.org/wiki/Method_overriding

Next >> Object Cloning In PHP

You might also like