PHP uses func_get_args () and func_num_args () functions to realize function overloading.
This article mainly explains "PHP uses func_get_args() and func_num_args() functions to implement overloading of functions", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. PHP uses func_get_args() and func_num_args() functions to overload functions.
1. You can use func_get_args() and func_num_args() to overload functions!!
PHP code:
The copy 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 '); //invoke func1
rewrite ('PHP ',' China'); //invoke func2
2. Use default values to get the desired result based on input:
The copy code is as follows:
function test($name="Xiao Li",$age="23"){
echo $name. " ".$ age;
}
test();
echo "";
test("a");
echo "";
test("a","b");
At this point, I believe that everyone has a deeper understanding of "PHP uses func_get_args() and func_num_args() functions to implement overloading of functions", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!