What is function overloading and overriding in PHP

Umesh Singh
3 min readMar 10, 2020

Method Overloading and Method overriding method is a very useful feature of any object-oriented programming language. In this section, we will discuss how to implement function overloading and function overriding in PHP. In object-oriented programming concept if functions of the class have the same name but different in parameters are termed as overloading and if the functions of the class are the same as well as parameter then it is termed as overriding.

Function Overloading

It contains the same function name and that function performs different tasks according to the number of arguments. For example, find the area of certain shapes where the radius is given then it should return the area of a circle if height and width are given then it should give the area of rectangle and others. Like other OOP languages, function overloading cannot be done by the native approach. In PHP overloading is done with the help of magic function. This function takes function arguments and name.

How to use overloading in PHP?

class MainClass {
public function ShowTitle($parameter1) {
echo “Best Interview Question”;
}
public function ShowTitle($parameter1, $parameter2) {
echo “BestInterviewQuestion.com”;
}

}
$object = new MainClass;
$object->ShowTitle(‘Hello’);

Output:

If you are looking PHP Interview Questions then you can visit here. These questions and answers will help you to crack your future interviews.

Fatal error: Cannot redeclare MainClass::ShowTitle()

class Resolve{
const Pi = 3.142 ;
function __call($fname, $arg){

if($name == ‘area’)
switch(count($arg)){

case 0 : return 0 ;
case 1 : return self::Pi * $arg[0] ;
case 2 : return $arg[0] * $ arg[1];
}

}

}
$circle = new Resolve();
echo “Area of circle:”.$circle->area(5).”</br>”;
$rect = new Resolve();
echo “Area of rectangle:”.$rect->area(5,10);

Output

Area of circle:15.71Area of rectangle:50

Function Overriding

It is the same as other OOPs programming languages. In this function, both parent and child classes should have the same function name and number of arguments. It is used to replace the parent method in child class. The purpose of function overriding is to change the behavior of the parent class method. The two functions with the same name and the same parameter are called function overriding.

class ParentClass {

function helloWorld() {

echo “Parent”;

}

}

class ChildClass extends ParentClass {

function helloWorld() {

echo “\nChild”;

}

}

$p = new ParentClass;

$c= new ChildClass;

$p->helloWorld();

$c->helloWorld();

Output:

Parent

Child

In PHP, there are various arrays functions that allow us to access and manipulate arrays. If you want to read these PHP array functions then you can visit here.

Conclusion

Function Overloading is defining methods that have similar signatures, yet have different parameters. Function Overriding is only pertinent to derived classes, where the parent class has defined a method and the derived class wishes to override that method. If you want to read more about OOPS Concepts then you can visit here.

We have a collections of Best Interview Questions and Answers that helps you to crack your future interviews

--

--