Function overloading and Overriding in PHP Last Updated : 18 Feb, 2022 Comments Improve Suggest changes Like Article Like 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. Comment More infoAdvertise with us Next Article Function overloading and Overriding in PHP ankit15697 Follow Improve Article Tags : Technical Scripter Web Technologies PHP PHP Programs PHP-function +1 More Similar Reads Covariance and Contravariance in PHP Covariance: In PHP, the return type of method which belongs to the child can be more specific compared to the return type of method of parent. This is done through covariance. For example, consider the parent class "Bank", and "SB" and "BOI" to be its children. php <?php // Parent class declared 3 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 b 1 min read How to Pass an Array into a Function in PHP ? This article will show you how to pass an array to function in PHP. When working with arrays, it is essential to understand how to pass them into functions for effective code organization and re-usability. This article explores various approaches to pass arrays into PHP functions, covering different 2 min read Anonymous recursive function in PHP Anonymous recursive function is a type of recursion in which function does not explicitly call another function by name. This can be done either comprehensively, by using a higher order function passing in a function as an argument and calling that function. It can be done implicitly, via reflection 2 min read How to find out where a function is defined using PHP ? When we do projects then it includes multiple modules where each module divided into multiple files and each file containing many lines of code. So when we declare a function somewhere in the files and forget that what the function was doing or want to change the code of that function but can't find 3 min read PHP | Check if a variable is a function To determine whether a passed argument is a function or not, Few of the most preferred methods are shown below. Using is_callable() Function: It is an inbuilt function in PHP which is used to verify the contents of a variable in called as a function. It can check that a simple variable contains the 3 min read PHP Callback Functions In PHP, the callback functions are related to the dynamic behavior and flexibility in code execution. They are used to pass custom logic or functions as arguments to other functions, letting developers change how a function behaves without changing its main structure. This makes the code more reusab 5 min read How to create optional arguments in PHP ? Arguments that do not stop the function from working even though there is nothing passed to it are known as optional arguments. Arguments whose presence is completely optional, their value will be taken by the program if provided. But if no value is given for a certain argument, the program will not 2 min read Multiple Inheritance in PHP Multiple Inheritance is the property of the Object Oriented Programming languages in which child class or sub class can inherit the properties of the multiple parent classes or super classes. PHP doesn't support multiple inheritance but by using Interfaces in PHP or using Traits in PHP instead of cl 4 min read PHP trait_exists() Function PHP implements a way to reuse code called Traits. The trait_exists() function in PHP is an inbuilt function that is used to check whether a trait exists or not. This function accepts the traitname and autoload as parameters and returns true if trait exists, false if not, null in case of an error. Sy 2 min read Like