Home  >  Article  >  Backend Development  >  PHP Tips: Function Overloading_PHP Tutorial

PHP Tips: Function Overloading_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:29:04711browse

1. You can use the two functions func_get_args() and func_num_args() to implement function overloading!!

PHP code:

Copy 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) {
fun c2($args[0], $args [1]);
                                                                {
echo $arg1, ' ', $arg2;
}
rewrite('PHP'); //Call func1
rewrite('PHP','China'); //Call func2



2. Use the default value to get the results you want based on the input:

Copy code

The code is as follows:

function test($name="Xiao Li",$age="23") { echo $name." ".$age; } }
test();
echo "
";
test("a") ;
echo "
";
test("a","b");





http://www.bkjia.com/PHPjc/779164.htmlwww.bkjia.com

truehttp: //www.bkjia.com/PHPjc/779164.htmlTechArticle1. You can use the two functions func_get_args() and func_num_args() to implement function overloading!! PHP code : Copy the code as follows: function rewrite() { $args = func_get_args(); if(func_...
Statement:
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