Home > Article > Backend Development > Introduction to how to implement overloading in php
This article mainly introduces the common methods of overloading in PHP. It analyzes four common implementation techniques of overloading in PHP by comparing it with Java in the form of examples. Friends in need can refer to the following
Examples of this article Common methods for implementing overloading in PHP. Share it with everyone for your reference, the details are as follows:
php is a weakly typed language and does not have overloading like a strongly typed language like JAVA.
Overloading generally means having the same function name or method name, but different parameter lists (including the number of parameters and parameter types)
From this definition, PHP does not Overloaded, because PHP does not allow the same function name to exist.
But not having it doesn’t mean it can’t be achieved.
php has four mechanisms to achieve overloading: (compared with java below)
1. Weak type, no need to define variable types
Let’s give an example of JAVA overloading:
class demo { public static void main (String[] args) { sum(1,1);//2 sum(1,1.5);//2.5 sum(1.5,1.5);//3.0 } public static void sum(int var1,int var2){ System.out.println(var1+var2); } public static void sum(int var1,double var2){ System.out.println(var1+var2); } public static void sum(double var1,double var2){ System.out.println(var1+var2); } }
If the above code is implemented in PHP language:
<?php function sum($var1,$var2){ echo $var1+$var2; } sum(1,1);//2 sum(1,1.5);//2.5 sum(1.5,1.5);//3 ?>
2. Optional parameters, allowing variables to set default values
JAVA overload example:
class demo { public static void main (String[] args) { sum(1,2);//3 sum(1,2,3);//6 } public static void sum(int var1,int var2){ System.out.println(var1+var2); } public static void sum(int var1,int var2,int var3){ System.out.println(var1+var2+var3); } }
Use php optional parameter feature to implement:
<?php function sum($var1,$var2,$var3=NULL){//$var3设置默认值 echo $var1+$var2+$var3; } sum(1,2);//2 sum(1,2,3);//2.5 ?>
You can see the same function, but it is easier to implement in a weakly typed language like php
3. Variable parameters
mainly use the following two functions:
func_num_args()
Return the number of parameters of the functionfunc_get_args()
Return an array containing the function parameter list
Specific implementation method:
<?php class demo{ public function method(){ $numargs = func_num_args();//获得参数数量 $args = func_get_args();//获得参数数组 switch ($numargs) {//通过变量个数来确定调用哪个方法 case 2: $this->method2($args[0],$args[1]); break; case 3: $this->method3($args[0],$args[1],$args[2]); break; } } private function method2($var1,$var2){ #code... } private function method3($var1,$var2,$var3){ #code... } } //调用 $obj = new demo(); $obj->method('var1','var2'); $obj->method('var1','var2','var3'); //看起来似乎通过一个接口调用 //但其实内部已经调用了各自定义的方法 ?>
4. Use the magic method __call()
public mixed __call ( string $name , array $arguments )
When calling an inaccessible method in an object, __call()
will be automatically called
$nameThe parameter is the name of the method to be called$argumentThe parameter is the parameter array of the called method
The specific implementation method is similar to the third point above:
<?php class demo{ public function __call($name,$arguments){ if($name == "method"){ switch (count($arguments)) {//获得参数数组个数 case 2: $this->method2($args[0],$args[1]); break; case 3: $this->method3($args[0],$args[1],$args[2]); break; } } } private function method2($var1,$var2){ #code... } private function method3($var1,$var2,$var3){ #code... } } //调用 $obj = new demo(); $obj->method("var1","var2"); $obj->method("var1","var2","var3"); ?>
The above is the detailed content of Introduction to how to implement overloading in php. For more information, please follow other related articles on the PHP Chinese website!