For a weak type language, PHP function overloading is not as common as OOP. However, the weak types are not differentiated, so they cannot be implemented through these methods. However, you can use the following method to implement simple pseudo-overload. The replacement method of function overload-pseudo-overload. let's take a look at the specific instance code.
<? Php // alternative to function overloading-pseudo-overload // indeed, function overloading is not found in PHP, which makes it impossible for us to handle it in many cases, sometimes you even have to define N parameters behind the function. // when you see func_get_arg, func_get_args, func_num_args, what do you think of when you think of these three functions? Function testOne ($ a) {echo ('just like this with a single parameter ');} function testTwo ($ a, $ B) {echo ('just like this with two parameters ');} function testThree ($ a, $ B, $ c) {echo ('Black and black, these are three parameters ');} function test () {$ argNum = func_num_args (); // in this section, you can use $ _ arg = func_get_args () to obtain all the parameters, just use an array. it is not convenient for me to express it below; $ I <$ argNum; $ I ++) {$ _ arg _ {$ I} = func_get_arg ($ I);} switch ($ argNum) {case 1: testOne ($ _ arg_1); bre Ak; 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, the most important thing is to use the class. // if these are used in the class, 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 = $ a;} function test2 ($ a, $ B) {$ this-> a = $ a; $ this-> B = $ B; }}?>
The above is all the content of this article. I hope you will like it.