PHP implements function overloading. Originally php? NamePHPPHP does not support function overloading! Php, as a weak type language, cannot directly implement overload like a strong type such as java or c ++. But you can use some php? Name = PHP "> PHP does not support function overloading!
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:
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:
Function test ($ name = "Xiao Li", $ age = "23") {echo $ name. "". $ age;} test (); echo"
"; Test (" a "); echo"
"; Test (" a "," B ");
Why php? Name = PHP "PHP does not support function overloading! Php, as a weak type language, cannot directly implement overload like a strong type such as java or c ++. However, you can use some...