Php uses func_get_arg, func_get_args, and func_num_args to implement pseudo-overload.

Source: Internet
Author: User

Php uses func_get_arg, func_get_args, and func_num_args to implement pseudo-overload.

I am interested in the problem of pseudo-heavy loading of php on the Internet. The authors will talk about how php uses func_get_arg, func_get_args, and func_num_args to implement pseudo-overload functions.

First, let's talk about the advantages of method overloading:

You do not need to write multiple functions 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. Although the method name is the same during the call, however, the corresponding function can be automatically called according to the parameter table. If we use reflector to view what Microsoft has written. net base class library, we can find that it uses a lot of method overloading, so we do not need to remember so many method names when calling, instead, you can pass different parameters directly by knowing the functions of the method. The compiler will clearly know which method we call.

However, 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 to solve related problems, php provides several functions, such as func_get_arg, func_get_args, and func_num_args, which can directly solve related problems. The sample code is as follows:

<? Phpfunction testOne ($ a) {echo ('one parameter is like this ');} function testTwo ($ a, $ B) {echo ('two parameters are like this ');} function testThree ($ a, $ B, $ c) {echo ('Ha, This is the three parameter ');} 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 not a parameter '); break ;}} /*** Implementation of the example */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 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? // The usage in the class is just 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 you may be interested in
  • Php uses the session_set_save_handler () function to save the session to the MySQL database.
  • PHP uses Curl Functions to capture webpages and download files with multiple threads
  • Performance Comparison of using in_array () foreach array_search () to find whether an array contains
  • Php uses array_flip to implement array key-value exchange to remove array duplicate values
  • Php uses the filter function to verify the mailbox, url, and IP address
  • Comparison of file_get_contents with curl in PHP
  • Use the PHP function memory_get_usage to obtain the current PHP memory consumption for program performance optimization.
  • Php searches for the existence of a value in the array (in_array (), array_search (), array_key_exists ())

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.