How to implement method overloading in PHP ? Last Updated : 30 Jun, 2021 Comments Improve Suggest changes Like Article Like Report 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 well as Overloading (Method and Function). Overloading: Overloading is an Object-Oriented concept in which two or more methods have the same method name with different arguments or parameters (compulsory) and return type (not necessary). It can be done as Constructor Overloading, Operator Overloading, and Method Overloading. In this article, we will be implementing method overloading in PHP. Method overloading in PHP: Example: Given below code will be showing some error. PHP <?php class GFG { function multiply($var1){ return $var1; } function multiply($var1,$var2){ return $var1 * $var1 ; } } $ob = new GFG(); $ob->multiply(3,6); ?> Output: The error is shown because we are redeclaring function multiply() in GFG Class. PHP Fatal error: Cannot redeclare GFG::multiply() Note: In other programming languages like C++, this will work for overloaded methods. To achieve method overloading in PHP, we have to utilize PHP's magic methods __call() to achieve method overloading. __call(): In PHP, If a class executes __call(), and if an object of that class is called with a method that doesn't exist then, __call() is called instead of that method. The following code demonstrates this. Example: PHP <?php class GFG { public function __call($member, $arguments) { $numberOfArguments = count($arguments); if (method_exists($this, $function = $member.$numberOfArguments)) { call_user_func_array(array($this, $function), $arguments); } } private function multiply($argument1) { echo $argument1; } private function multiply2($argument1, $argument2) { echo $argument1 * $argument2; } } $class = new GFG; $class->multiply(2); $class->multiply(5, 7); ?> Output: 35 Comment More infoAdvertise with us Next Article How to implement method overloading in PHP ? atul07 Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-Questions Similar Reads Method Overloading In Ruby What is method overloading? Method overloading is a feature that allows a class to have more than one method with same name but different method signature ie. different number of arguments, order of arguments and different types of parameters. Return type is not included in method signature, thus ov 4 min read How to overload and override main method in Java How to overload main method in java? Method Overloading can be defined as a feature in which a class can have more than one method having the same name if and only if they differ by number of parameters or the type of parameters or both, then they may or may not have same return type. Method overloa 4 min read Difference between Multi-methods and Overloading In multi-methods we use the collection of methods. And of all methods have the same name, the same number of arguments, and can have overlapping type signatures. In the case of multi-methods when a function call is made then a collection of all the methods is considered as possible methods. Dependi 4 min read Perl | Method Overriding in OOPs In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signat 6 min read Operator Overloading in Programming Operator Overloading is a feature in some programming languages used to redefine or "overload" the standard behavior of operators (such as +, -, *, etc.) to work with user-defined data types. This is useful when working with objects of custom classes. In this article, we will learn about the basics 4 min read PHP | ReflectionClass implementsInterface() Function The ReflectionClass::implementsInterface() function is an inbuilt function in PHP which is used to check the specified interface is present or not. Syntax: bool ReflectionClass::implementsInterface( string $interface ) Parameters: This function accepts a single parameter $interface which holds the s 2 min read Solidity Function Overloading Function overloading in Solidity lets you specify numerous functions with the same name but varying argument types and numbers.Solidity searches for a function with the same name and parameter types when you call a function with certain parameters. Calls the matching function. Compilation errors occ 1 min read PHP | ReflectionMethod __toString() Function The ReflectionMethod::__toString() function is an inbuilt function in PHP which is used to return the string representation of the specified method object. Syntax: string ReflectionMethod::__toString ( void ) Parameters: This function does not accept any parameter. Return Value: This function return 2 min read Variable-length argument list in PHP Given a set of arguments whose length is unknown at present, we will see how a function can work with these unknown numbers of arguments whose quantity will vary depending on the requirement. We will take up each word one by one to deeply understand the topic we are dealing with. Variable: It is the 2 min read What are magic methods and how to use them in PHP ? PHP magic methods are special methods that are called automatically when certain conditions are met. There are several magic methods in PHP. Every magic method follows certain rules - Every magic method starts with a double underscore ( __ ).They are predefined and neither can be created nor removed 4 min read Like