Open In App

Function overloading and Overriding in PHP

Last Updated : 18 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Function overloading and overriding is the OOPs feature in PHP. In function overloading, more than one function can have same method signature but different number of arguments. But in case of function overriding, more than one functions will have same method signature and number of arguments. 
Function Overloading: Function overloading contains same function name and that function performs different task according to number of arguments. For example, find the area of certain shapes where radius are given then it should return area of circle if height and width are given then it should give area of rectangle and others. Like other OOP languages function overloading can not be done by native approach. In PHP function overloading is done with the help of magic function __call(). This function takes function name and arguments. 
Example: 
 

php




<?php
// PHP program to explain function
// overloading in PHP
 
// Creating a class of type shape
class shape {
     
    // __call is magic function which accepts
    // function name and arguments
    function __call($name_of_function, $arguments) {
             
        // It will match the function name
        if($name_of_function == 'area') {
             
            switch (count($arguments)) {
                     
                // If there is only one argument
                // area of circle
                case 1:
                    return 3.14 * $arguments[0];
                         
                // IF two arguments then area is rectangle;
                case 2:
                    return $arguments[0]*$arguments[1];
            }
        }
    }
}
     
// Declaring a shape type object
$s = new Shape;
     
// Function call
echo($s->area(2));
echo "\n";
     
// calling area method for rectangle
echo ($s->area(4, 2));
?>


Output: 

6.28
8

 

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

php




<?php
// PHP program to implement
// function overriding
 
// This is parent class
class P {
     
    // Function geeks of parent class
    function geeks() {
        echo "Parent";
    }
}
 
// This is child class
class C extends P {
     
    // Overriding geeks method
    function geeks() {
        echo "\nChild";
    }
}
 
// Reference type of parent
$p = new P;
 
// Reference type of child
$c= new C;
 
// print Parent
$p->geeks();
 
// Print child
$c->geeks();
?>


Output: 

Parent
Child

 

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.



Similar Reads

How to prevent overriding using Immediately Invoked Function Expression in JavaScript ?
Overriding is basically when you define multiple functions or variables that have the same name, the last one defined will override all the previously defined ones and every time when you invoke a function, the last defined one will get executed. Overriding usually happens when you have multiple javascript files in your page. It can be an external
2 min read
How to prevent overriding using fake namespace in JavaScript ?
Overriding is basically when you define multiple functions or variables that have the same name, the last one defined will override all the previously defined ones and every time when you invoke a function, the last defined one will get executed. Overriding usually happens when you have multiple javascript files in your page. It can be an external
2 min read
Method Overriding in TypeScript
In this article, we will understand some of the necessary details which are associated with Method Overriding, further, we will see how to implement Method Overriding in TypeScript. Method Overriding: Method Overriding is the process in which a method belonging to the base (or parent) class is overridden by the same method (same method and signatur
3 min read
How CSS style overriding works ?
In this article, we are going to learn how CSS style overriding works. Cascading Style Sheets referred to as CSS, is a simply designed language intended to simplify the process of making web pages presentable. CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this independent of the HTML that makes up each web pag
3 min read
Overloading in PHP
What is function overloading? Function overloading is the ability to create multiple functions of the same name with different implementations. Function overloading in PHP? Function overloading in PHP is used to dynamically create properties and methods. These dynamic entities are processed by magic methods which can be used in a class for various
4 min read
How to implement method overloading in PHP ?
PHP stands for Hypertext Preprocessor, it is a popular general-purpose scripting language that is mostly used in web development. It is fast, flexible, and pragmatic and the latest versions of PHP are object-oriented which means you can write classes, use inheritance, Polymorphism, Data Abstraction, Encapsulation, Constructor, Destructor, and as we
2 min read
How to achieve function overloading in TypeScript ?
In this article, we will try to understand some basic details which are associated with the concept of function/method overloading, further will see how we could implement function overloading in TypeScript. Let us first understand some basic facts involved in function/method Overloading. Function/Method Overloading: Method overloading is the proce
2 min read
Syntax to create function overloading in TypeScript
Function Overloading: Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters and also the data types of all the parameters in different functions would be different as well . When a function name is overloaded with different jobs/ requirements it is thus referred
2 min read
Function Overloading in JavaScript
Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading “Function” name should be the same and the arguments should be different. Unlike other program
4 min read
How to get the function name inside a function in PHP ?
To get the function name inside the PHP function we need to use Magic constants(__FUNCTION__). Magic constants: Magic constants are the predefined constants in PHP which is used on the basis of their use. These constants are starts and end with a double underscore (__). These constants are created by various extensions. Syntax: $string = __FUNCTION
1 min read