Occasionally on the Internet to see the problem of the pseudo-overload of PHP, a bit interested in the study. The following author will talk about how PHP uses Func_get_arg,func_get_args,func_num_args to implement pseudo overload problems with functions.
First, the benefits of method overloading:
The implementation overload can write more than one function for different parameter types or number of parameters. Multiple functions use the same name, but the parameter table, that is, the number of parameters or (and) data types can be different, when invoked, although the method name is the same, but according to the parameter table can automatically call the corresponding function. If we use reflector to view the base Class library of. NET written by Microsoft, we can see that he uses a lot of method overloads so that when we call, we do not need to remember so many method names, but we know the function of the method can directly pass different parameters to him, the compiler will know exactly which method we call.
But there is no function overload in PHP this concept, so many times we can not do some processing, and even sometimes have to define a good n parameters after the function to solve the problem, and PHP provides several functions, such as: func_get_arg,func_get_args,func_ Num_args can directly solve related problems. The example code is as follows:
<?php function Testone ($a) {echo (' One argument is so ');} function Testtwo ($a, $b) {echo (' two arguments in this way ');} function Testthre
E ($a, $b, $c) {echo (' Ha, this is three parameters ');} function Test () {$argNum = Func_num_args (); This paragraph can actually use $_arg = Func_get_args () to get all the parameters, just to use the array, not convenient for my expression below, hehe for ($i = 0; $i < $argNum; $i + +) {$_arg_{$i} = f
Unc_get_arg ($i);
Switch ($argNum) {case 1:testone ($_arg_1);
break;
Case 2:testtwo ($_arg_1, $_arg_2);
break;
Case 3:testthree ($_arg_1, $_arg_2, $_arg_3);
break;
Default:echo (' This is the case without parameters ');
break;
/** * Example Implementation */test ();
Echo (' <br> ');
Test (1);
Echo (' <br> ');
Test (1, 2);
Echo (' <br> ');
Test (1, 2, 3);
These are only used in functions, in fact, the most important or in the use of classes//If these are used in the class I do not need to worry about whether the constructor has a few parameters, is not it?
The use of the class inside only give a simple example class test{var $a = 0;
var $b = 0;
function test () {$argNum = Func_num_args ();
$_arg = Func_get_args ();
Switch ($argNum) {Case 1: $this->test1 ($_arg[0]);
break; Case 2: $this-≫test2 ($_arg[0], $_arg[1]);
Break
Default: $this->a = 0;
$this->b = 1;
Break
} function Test1 ($a) {$this->a = $a;
function test2 ($a, $b) {$this->a = $a;
$this->b = $b; }
}
Articles that you may be interested in
- PHP uses the Session_set_save_handler () function to save session to MySQL database
- PHP uses Curl functions to implement multi-threaded crawl Web pages and download files
- PHP uses Array_flip to implement array key exchange to remove duplicate value of array
- About performance comparisons using In_array () foreach Array_search () to find out if an array is contained
- How PHP uses the filter function to verify mailboxes, URLs, and IP addresses
- Use PHP function memory_get_usage to get current PHP memory consumption to achieve program performance optimization
- PHP finds whether a value exists in the array (In_array (), Array_search (), array_key_exists ())
- Comparison of performance efficiencies in PHP file_get_contents to curl