Function overloading and Overriding in PHP - GeeksforGeeks

18 Feb, 2022
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 shapeclass 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 callecho($s->area(2));echo "\n";     // calling area method for rectangleecho ($s->area(4, 2));?>
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 classclass P {         // Function geeks of parent class    function geeks() {        echo "Parent";    }} // This is child classclass 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();?>
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.
adnanirshad158
anikaseth98
surinderdawra388
PHP-function
PHP
PHP Programs
Technical Scripter
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
Split a comma delimited string into an array in PHP
How to pass JavaScript variables to PHP ?
How to get parameters from a URL string in PHP?
How to convert array to string in PHP ?
How to call PHP function on the click of a Button ?
Split a comma delimited string into an array in PHP
How to pass JavaScript variables to PHP ?
How to get parameters from a URL string in PHP?