Who can talk about the use of __call?

Source: Internet
Author: User
__call only two parameters? Why does this code pass several parameters?
I see someone say that. __call the first parameter, $m, is the method you want to call test. I can't understand that. The first parameter is not a 1?


  
   x;    }}$foo = new Caller();$a = $foo->test(1, "2", 3.4, true);var_dump($a);?>

Reply content:

__call only two parameters? Why does this code pass several parameters?
I see someone say that. __call the first parameter, $m, is the method you want to call test. I can't understand that. The first parameter is not a 1?


  
   x;    }}$foo = new Caller();$a = $foo->test(1, "2", 3.4, true);var_dump($a);?>

__callis called when an undefined method is called.

That is, if your test method is undefined, the test method name is passed in as __call the first argument, and test the parameter is passed in as __call the second parameter in the array.

So when you call $foo->test(1, "2", 3.4, true) , it's actually the equivalent of a call $foo->__call('test', array(1, "2", 3.4, true)) .

The __call method is triggered when a method of the class is called, such as:


  
   "后面的字符串,$parameters是通过这个方法传过来的参数    }}$google = new google();$keyword = 'VR';$google->search($keyword);//当调用当前对象不存在的方法时,会转向__call$google->operate();

With __call, you can do some encapsulation to invoke other objects and methods.
Attached official reference: PHP Magic method

The first parameter is the function name of the non-existent method that you call, and the second is a pair of arguments that you call a non-existent method simultaneous

  • 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.