Php, as a weak type language, cannot directly implement overload like a strong type such as java or c ++. However, some methods can be used to indirectly implement overloading. 1. you can use the func_get_args () and func_num_args () functions to overload the functions !!
PHP code:
The code is as follows:
Function rewrite (){
$ Args = func_get_args ();
If (func_num_args () = 1 ){
Func1 ($ args [0]);
} Else if (func_num_args () = 2 ){
Func2 ($ args [0], $ args [1]);
}
}
Function func1 ($ arg ){
Echo $ arg;
}
Function func2 ($ arg1, $ arg2 ){
Echo $ arg1, '', $ arg2;
}
Rewrite ('php'); // call func1
Rewrite ('php', 'China'); // call func2
2. use the default value to obtain the desired result based on the input:
The code is as follows:
Function test ($ name = "Xiao Li", $ age = "23 "){
Echo $ name. "". $ age;
}
Test ();
Echo"
";
Test ("");
Echo"
";
Test ("a", "B ");