Alternative to php function overloading-Explanation of pseudo-Reloading

Source: Internet
Author: User
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.

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 see 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); 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, 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 = $; $ this-> B = $ B ;}}?>

The above is all the content of this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.