Method overloading simple example in php

Method overloading simple example in php

Introduction to Method Overloading in PHP

Method Overloading is one type of Overloading other than Property Overloading. It is to create one/many dynamic methods that are not created within that class scope/scopes. PHP method overloading concept also helps in triggering the magic methods which are dictated for the appropriate purpose. Apart from the property overloading concept, the PHP method’s overloading concept allows function call/calls on both the object and also the static context. Basically is one of the methods of OOPs.

Syntax:

Public _call (string $name1 , array $arguments1 ) : mixed
Public static _callStatic (string $name1 , array $arguments1 ) : mixed

How does Method Overloading work in PHP?

The Method Overloading works with the declaration inside the class by creating dynamic methods. It also works by triggering some magic methods for an appropriate purpose and it also calls function/function calls on both the static context and the object. Method Overloading concept is also fine with most of the other programming languages like c, java, etc.. Method Overloading concept is also called a static polymorphic concept.

There are some of the magic functions, they are:

  • _call(): This call() magic function will be triggered in order to invoke the overloaded method/methods which are in the object context.
  • _callStatic(): This callstatic() magic function will also be triggered in order to invoke the overloaded concepts/methods which are in the static context.

Examples of Method Overloading in PHP

Here are the examples of Method Overloading in PHP mention below

Example #1

The $name1 argument which is in the below PHP programming language is the name of the method which is to be called whereas $arguments are one of the enumerated arrays which contain the parameters/arguments which are used to pass to the $name ’ed method.

_call() function used using 2 parameters $name1 and $arguments1. Implode() function actually returns string from the array elements i.e., from the string/sentence. In Implode(separator, array), the separator is the optional parameter but it just a recommendation to use both of the parameters for backward compatibility. The specific type of separator in the separator parameter will insert separator to the words/strings which are present in the array parameter.

The Obj variable will create a new object called SPK. Obj-> will helps in order to access the methods and the properties of the object. Spk will execute from the static context whereas the obj will run from the object context.

Code:

runTest('in one of the object context');
SPK::runTest('in one of the static context');
?>

Output:

Method overloading simple example in php

Example #2

In the below example, the foo1 class is created with only the _call() function with the die() function in order to print a message and also exits from the current program/PHP script. Die() is the same as the exit() function which accepts just one parameter inside of its a parenthesis.

Foo1-> will helps in order to access the methods and the properties of the object from the variable $foo1.

Code:

{' wow !'}();
// outputs ' wow !'
?>

Output:

Method overloading simple example in php

Example #3

This is the example of method overloading in the PHP programming language using the call() function and private/protected methods.

Here calling the private/protected methods is done by accessing by the typo or something etc..

Echo _METHOD_PHP_EOL will return what type of method is used. It is done by using only the _call() function which runs from the object context.

Code:

$method1();
}
}
protected function bar1()
{
echo __METHOD__.PHP_EOL;
}
private function baz1()
{
echo __METHOD__.PHP_EOL;
}
}
$test    =    new TestMagicCallMethod1();
$test->foo1();
$test->bar1();
$test->baz1();
?>

Output:

Method overloading simple example in php

Example #4

This is the program of call() and call static() functions concept which is used for method overloading concept. Here in this PHP program which is below will call _call() function first before the _callstatic() function which is in the instance.

Var dump() will provide information about the variable which is in the parenthesis in PHP & some of the other object-oriented programming languages too. Other than that everything is the same just like the above examples.

Code:

who();
}
public static function __callStatic($a1, $b1) {
var_dump('A1 static');
}
public function __call($a1, $b1) {
var_dump('A1 call');
}
}
$a1 = new A1;
$a1->test1();
?>

Output:

Method overloading simple example in php

Example #5

This is the example of _call() function if the object’s class is called with the method which doesn’t even exist then _call() function’s concept is called instead of the method.

In the below PHP Program, area() method which is created as dynamic to execute in order to help the magic method of method overloading concept called _call() function. Its behavior will change based on the parameters of the object which are passed.

Code:

area1(3);
$rect1 = new Shape1();
echo $rect1->area1(8,6);
?>

Output:

Method overloading simple example in php

Example #6

Here both the _call() and _callstatic() functions are used just like the 1st example.

Code:

overloaded_method();
Toys1::overloaded_property();
?>

Output:

Method overloading simple example in php

Example #7

Call() function of Method Overloading triggered and invoked the inaccessible methods which are in the object context. Call() is mixed with the syntax _call(string $name1 , array $arguments1).

Then $name1 parameter is for the name of the method which is to be called whereas the array $arguments1 is the parameter which is an enumerated array which contains/has the parameters which are to be passed to the $name variables method.

Code:

displayMessage12($arguments1[0],$arguments1[1]);
}
elseif (count($arguments1) === 1) {
$this->displayMessage11($arguments1[0]);
} else {
echo "\n unknown method";
return false;
}
}
function displayMessage11($var11) {
echo "\n from func1($var11)";
}
function displayMessage12($var11,$var12) {
echo "\n from func2($var11,$var12)";
}
}
$obj1 = new ABC1;
$obj1->displayMessage11('hello');
$obj1->displayMessage12('hello','hello2');
$obj1->displayMessage13('Hello');
?>

Output:

Method overloading simple example in php

Example #8

It is also just like the first example program. Check it once.

Code:

DemoTest1();
// Now using the static's reference
MethodOverloading1::DemoTest1();
?>

Output:

Method overloading simple example in php

Example #9

This is the program to show the area of the circle and the rectangle using some parameters and call() function of the method overloading concept. Only with the object context, the program will run due to the object assigning of an object variable to the class, etc.

Code:

area1(15); // display output of the area of circle
$rect1 = new TDshape1();
echo "\n Area of the rectangle:".$rect1->area1(5,11); // display output of the area of rectangle
?>

Output:

Method overloading simple example in php

This is a guide to Method Overloading in PHP. Here we discuss the basic concept, how does Method Overloading work in PHP along with various examples. You may also have a look at the following articles to learn more –

  1. Prime Numbers in PHP
  2. Multiple Inheritance in PHP
  3. Calendar in PHP
  4. Private in PHP

What is method overloading in PHP with example?

Method Overloading: It is a type of overloading for creating dynamic methods that are not declared within the class scope. PHP method overloading also triggers magic methods dedicated to the appropriate purpose. Unlike property overloading, PHP method overloading allows function call on both object and static context.

Can we do method overloading in PHP?

You cannot overload PHP functions. Function signatures are based only on their names and do not include argument lists, so you cannot have two functions with the same name. Class method overloading is different in PHP than in many other languages.

What is an example of method overloading?

In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading. For example: void func() { ... }

What is Overloading and overriding in PHP with example?

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.