Method Overloading in PHP

PHPz
Release: 2024-08-29 12:59:19
Original
849 people have browsed it

Method Overloading is one type of Overloading other than Property Overloading. It is to create one/many dynamic methods not created within that class scope/scope. PHP method overloading concept also helps trigger the magic methods 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 the static context. Basically is one of the methods of OOPs.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

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

How does Method Overloading work in PHP?

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 calls function/function calls on both the static context and the object. Method Overloading concept is also fine with most other programming languages like c, java, etc. We refer to Method Overloading as static polymorphism.

There are some of the magic functions, they are:

  • _call(): An object’s context can trigger the execution of overloaded methods using the call() function.
  • _callStatic(): The callstatic() magic function activates the overloaded concepts/methods in the static context.

Examples of Method Overloading in PHP

Here are the examples of Method Overloading in PHP mentioned below

Example #1

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

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

The Obj variable will create a new object called SPK. Obj-> will help to access the object’s methods and properties. Spk will execute from the static context, whereas the obj will run from the object context.

Code:

<?php
class SPK {
public function __call($name1, $arguments1) {
echo "object method calling '$name1' "
. implode(', ', $arguments1). "\n";
}
public static function __callStatic($name1, $arguments1) {
echo "static method Calling '$name1' "
. implode(', ', $arguments1). "\n";
}
}
// Create new object
$obj = new SPK;
$obj->runTest('in one of the object context');
SPK::runTest('in one of the static context');
?>
Copy after login

Output:

Method Overloading in PHP

Example #2

The code example defines the foo1 class with a single _call() function that executes the die() function to display a message and terminate the current PHP script. Die() is the same as the exit() function, which accepts just one parameter inside its parenthesis.

Foo1-> will help to access the object’s methods and properties from the variable $foo1.

Code:

<?php
class Foo1 {
function __call($m1, $a1) {
die($m1);
}
}
$foo1 = new Foo1;
print $foo1->{' wow !'}();
// outputs ' wow !'
?>
Copy after login

Output:

Method Overloading in PHP

Example #3

This is an 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 uses only the _call() function, which runs from the object context.

Code:

<?php
class TestMagicCallMethod1 {
public function foo1()
{
echo __METHOD__.PHP_EOL;
}
public function __call($method1, $args1)
{
echo __METHOD__.PHP_EOL;
if(method_exists($this, $method1))
{
$this->$method1();
}
}
protected function bar1()
{
echo __METHOD__.PHP_EOL;
}
private function baz1()
{
echo __METHOD__.PHP_EOL;
}
}
$test    =    new TestMagicCallMethod1();
$test->foo1();
$test->bar1();
$test->baz1();
?>
Copy after login

Output:

Method Overloading in PHP

Example #4

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

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

Code:

<?php
class A1 {
public function test1 () {
static::who();
A1::who();
self::who();
$this->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();
?>
Copy after login

Output:

Method Overloading in PHP

Example #5

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

Execute the _call() function to support the concept of method overloading through the dynamic area() method included in the PHP program below. The object’s behavior will vary depending on the parameters that pass to it.

Code:

<?php
class Shape1 {
const PI1 = 3.142 ;
function __call($name1,$arg1){
if($name1 == 'area1')
switch(count($arg1)){
case 0 : return 0 ;
case 1 : return self::PI1 * $arg1[0] ;
case 2 : return $arg1[0] * $arg1[1];
}
}
}
$circle1 = new Shape1();
echo $circle1->area1(3);
$rect1 = new Shape1();
echo $rect1->area1(8,6);
?>
Copy after login

Output:

Method Overloading in PHP

Example #6

Here, the _call() and _callstatic() functions are used like in the 1st example.

Code:

<?php
class Toys1
{
public function __call($name1,$pavan1){
echo "Magic method invoked while method overloading with object reference";
}
public static function __callStatic($name1,$pavan1){
echo "Magic method invoked while method overloading with static access";
}
}
$objToys1 = new Toys1;
$objToys1->overloaded_method();
Toys1::overloaded_property();
?>
Copy after login

Output:

Method Overloading in PHP

Example #7

The call () function of method Overloading triggered and invoked the inaccessible methods 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 that is an enumerated array that contains/has the parameters which are to be passed to the $name variables method.

Code:

<?php
class ABC1 {
public function __call($method_name1, $arguments1) {
$methodArray1 = array('displayMessage11','displayMessage12');
if (in_array($method_name1,$methodArray1) === false) {
die("\n Method does not exist");
}
if (count($arguments1) === 2) {
$this->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');
?>
Copy after login

Output:

Method Overloading in PHP

Example #8

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

Code:

<?php
class MethodOverloading1
{
public function __call($name1,$pavan1){
echo "\n--It is now With the object reference ";
}
public static function __callStatic($name1,$pavan1){
echo "\n-----It is now With the static reference \n";
}
}
// Here now creating the object of the class " MethodOverloading "
$obj1 = new MethodOverloading1;
echo "Method Overloading1 Now in Command ";
// Now using the object's reference
$obj1->DemoTest1();
// Now using the static's reference
MethodOverloading1::DemoTest1();
?>
Copy after login

Output:

Method Overloading in PHP

Example #9

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

Code:

<?php
class TDshape1 {
const Pi1 = 3.142 ;  // constant value
function __call($fname1, $argument1){
if($fname1 == 'area1')
switch(count($argument1)){
case 0 : return 0 ;
case 1 : return self::Pi1 * $argument1[0] ; // 3.14 * 15
case 2 : return $argument1[0] * $argument1[1];  // 5 * 11
}
}
}
$circle1 = new TDshape1();
echo "Area of the circle:".$circle1->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
?>
Copy after login

Output:

Method Overloading in PHP

The above is the detailed content of Method Overloading in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!