PHP & Frameworks

Function Overloading and Overriding in PHP

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.

__call():

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.

Example

<?php
   class Shape {
      const PI = 3.142 ;
      function __call($name,$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 Shape();
   echo $circle->area(3);
   $rect = new Shape();
   echo $rect->area(8,6);
?>

Output : This will produce the following output

9.42648
48

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.

<?php
   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;
   $ob->demo();
   $ob->display();
   $ob2 = new Derived;
   $ob2->demo();
   $ob2->display();
?>

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

Conclusion

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.

Developer Diary

Share
Published by
Developer Diary
Tags: PHP Tips

Recent Posts

Git Tag Cheat Sheet

Introduction Git tags are an essential feature of version control systems, offering a simple way…

2 months ago

Understanding Web Storage: Cookies, Local Storage

Introduction The methods that browsers employ to store data on a user's device are referred…

3 months ago

Setting up OpenVPN Access Server in Amazon VPC – AWS

Introduction A well-known open-source VPN technology, OpenVPN provides strong protection for both people and businesses.…

3 months ago

Enhance Error Tracking & Monitoring: Integrate Sentry with Node.js & Express.js

Introduction Integrating Sentry into a Node.js, Express.js, and MongoDB backend project significantly enhances error tracking…

3 months ago

Comparing Callbacks, Promises, and Async/Await in JavaScript

Introduction In the world of JavaScript development, efficiently managing asynchronous operations is essential. Asynchronous programming…

5 months ago

How To Secure Nginx with Let’s Encrypt on Ubuntu EC2 Instance

Introduction Let's Encrypt is a Certificate Authority (CA) that makes it simple to obtain and…

7 months ago