Skip to content

Function Overloading and Overriding in PHP

function overloading and overriding

Function overloading and overriding is the hallmark of object-oriented programming in PHP. Function overloading means that multiple functions have the same name but different parameters. But in the case of a function override, more than one function will have the same method signature and number of arguments.

PHP doesn’t support function overloading directly like other languages ​​like C++, JAVA etc. But we can solve this problem by using PHP’s magic __call() method. So let’s see how overloading and overriding works in PHP.

Function Overloading in PHP

The function overloading contains the same function name, and this function performs a different task depending on the number of arguments.

Let’s discuss how to achieve overloading in PHP. In the case of PHP, we need to use the PHP __call() magic methods to achieve method overloading. In PHP, overloading means that the behavior of the method changes dynamically depending on the input parameters. In this tutorial we will understand these perceptions. Let’s analyze the __call() method.


If a class does __call(), then if an object of that class is called with a method that doesn’t exist, __call() will be called instead of that method.


   class Shape {
      const PI = 3.142 ;
      function __call($name,$arg){
         if($name == 'area')
               case 0 : return 0 ;
               case 1 : return self::PI * $arg[0] ;
               case 2 : return $arg[0] * $arg[1];
   $circle = new Shape();
   echo $circle->area(3);
   $rect = new Shape();
   echo $rect->area(8,6);

Output : This will produce the following output


Function Overriding in PHP

Function overriding in PHP is quite easy. Overriding is the process of modifying something of the inherited method. Means in function overriding, the parent and child classes have the same function name with and number of arguments

In belwo example you see two classes Base and Derived. Derived cass extends to Base. Which means Derived can access parent class. Here you see parent has demo() function and child class also demo() function with same name and same argument. Here you see function overriding scenario where child class override the base class function.

   class Base {
      function display() {
         echo "\nBase class function declared final!";
      function demo() {
         echo "\nBase class function!";
   class Derived extends Base {
      function demo() {
         echo "\nDerived class function!";
   $ob = new Base;
   $ob2 = new Derived;

Output : This will produce the following output

Base class function!
Base class function declared final!
Derived class function!
Base class function declared final!

More Interview Questions

PHP OOPS Interview Questions And Answers (2022)

PHP Interview Questions And Answers

Node.Js Interview Questions And Answers

CodeIgniter Interview Questions And Answers

JavaScript Interview Questions And Answers

Latest MySQL Interview Questions And Answers

Laravel Interview Questions And Answers


In this article we understand the function overloading and overriding with the help of example. if you have any issues for understanding please comment I will try to give reply ASAP.