Workaround for function overloading-pseudo overload // Indeed, there is no concept of function overloading in PHP, so many times we are unable to do some processing, and sometimes we have to define N parameters after the function. What do you think of when you see Func_get_arg,func_get_args,func_num_args, these three functions? function Testone ($a) { Echo (' A parameter just so '); } function Testtwo ($a, $b) { Echo (' Two parameters in this case '); } function Testthree ($a, $b, $c) { Echo (' Black black, 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 my following expression, hehe 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 without parameters '); break; } } Test (); Echo ('); Test (1); Echo ('); Test (1, 2); Echo ('); Test (1, 2, 3); These are only used in the function, in fact, the most important or in the application of the class If this is used in a class, I don't have to worry about whether the constructor has a few arguments, does it? Class Test { var $a = 0; var $b = 0; function Test () { $argNum = Func_num_args (); $_arg = Func_get_args (); Switch ($argNum) { Case 1: Test1, $this ($_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; } } ?> |