|
<? Php
// Alternative to function overload-pseudo-Overload
//
// Indeed, there is no function overload concept in PHP, which makes it impossible for us to perform some processing in many cases. Sometimes we have to define N parameters after the function.
// When you see func_get_arg, func_get_args, func_num_args, do you think of these three functions?
Function testOne ($ ){
Echo ('one parameter is like this ');
}
Function testTwo ($ a, $ B ){
Echo ('two parameters are like this ');
}
Function testThree ($ a, $ B, $ c ){
Echo ('black and black, these are three parameters ');
}
Function test (){
$ ArgNum = func_num_args ();
// You can use $ _ arg = func_get_args () to obtain all the parameters in this section. It is just an array. It is not convenient for me to express it below.
For ($ I = 0; $ I <$ argNum; $ I ++ ){
$ _ Arg _ {$ I} = func_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 with no arguments ');
Break;
}
}
Test ();
Echo ('');
Test (1 );
Echo ('');
Test (1, 2 );
Echo ('');
Test (1, 2, 3 );
// These are only used in functions. In fact, they are mainly used in classes.
// If these classes are used, I don't need to worry about whether the constructor has several parameters, do I?
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 ($ ){
$ This-> a = $;
}
Function test2 ($ a, $ B ){
$ This-> a = $;
$ This-> B = $ B;
}
}
?>
|