Php_call () and call_user_func_array ()

Source: Internet
Author: User
Php _ call () and call_user_func_array () comprehension 1. mixed _ call (stringname, arrayarguments) Themagicmethod _ call () using php _ call () and call_user_func_array ()
1. mixed _ call (string name, array arguments)

The magic method _ call () allows to capture invocation of non existing methods. that way _ call () can be used to implement user defined method handling that depends on the name of the actual method being called. this is for instance useful for proxy implementations. the arguments that were passed in the function will be defined as an array in the $ arguments parameter. the value returned from the _ call () method will be returned to the caller of the method.

This magic method allows users to call methods that do not exist in a class. it is used to implement methods that depend on the real method names when called. A typical example is to implement proxy. the $ arguments parameter of the method is an array, and the return value of __call () is returned to the method caller.

In other words, this method is mainly used to call dynamic methods. if another class defines the _ call () method, when the user calls a non-existent method of this class, he can use the method name and parameter of the nonexistent method called to perform operations defined by the user in the _ call () method body. in this case, _ call () the method parameter is the method name and parameter of the nonexistent method called.

Example

 ';echo 'and the arguments are 
';var_dump( $args );}}$person = new Person();$person->test( 1 , TRUE );?>


Program output

Reference you call method test
And the arguments are
Array
0 => int 1
1 => boolean true



2. mixed call_user_func_array (callback function, array param_arr)


Call a user defined function with the parameters in param_arr.

Parameters


Function
The function to be called.

Param_arr
The parameters to be passed to the function, as an indexed array.


Return value
Returns the function result, or FALSE on error.

This method can be used to call a method dynamically by passing in the class name and method parameters in the class.

Example

 ';echo 'and the arguments are 
';var_dump( $args );}} $person = new Person();call_user_func_array( array( $person , 'talk' ) , array( 'hello' ) );?>


Program output

Reference hello


The two methods are shared to implement the proxy model.

class Person{function talk( $sound ){echo $sound;}function __call( $method , $args ){echo 'you call method ' . $method . '
';echo 'and the arguments are
';var_dump( $args );}}class PersonProxy{private $person;function __construct(){$this->person = new Person();}function __call( $method , $args ){call_user_func_array( array( $this->person , $method ) , $args );}}$person_proxy = new PersonProxy(); $person_proxy->talk( 'thank you' );


Program output

Reference thank you, chinagdvea, 1st Floor, 2011-12-07

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.