PHP Functions Call_user_func & Call_user_func_array

Source: Internet
Author: User

Call_user_func_array (callable $callback, array $param _arr)

Parameter 1: Call a callback function,

Parameter 2: The array parameter is the parameter of the callback function.


Call_user_func (callable $callback, $mixed $parameter, $mixed $ ...)

Parameter 1: called callback function

Parameter 2-n: The parameter of the callback function.


Compare the differences between the two. The second argument of the former must be an array.


Case one: Call the normal function.

<?phpfunction Barber ($type) {    

Here, the first parameter directly specifies the name of the function.

<?phpfunction Barber ($type) {    

Or compare the differences between the two.

Look out please continue.


Scenario Two: Call a static function in the class.

Here's Call_user_func_arr.

Namespace Foo;class F {public static function ShowName ($name) {return strtoupper ($name);}} Echo Call_user_func_array (__namespace__. ' \f::showname ', Array (' vein ')); Echo Call_user_func_array (Array (__namespace__. ' \f ', ' showname '), Array (' vein '), $f = new F (), Echo Call_user_func_array (Array ($f, ' showname '), Array (' vein '));

Here's call_user_func.

Namespace Foo;class F {public static function ShowName ($name) {return strtoupper ($name);}} Echo Call_user_func (__namespace__. ' \f::showname ', ' vein '); Echo call_user_func (Array (__namespace__. ' \f ', ' showname '), ' vein '); $f = new F (); Echo call_user_func (Array ($f, ' showname '), ' vein ');

Scenario Three: Dynamic method invocation

Call_user_func

Namespace Foo;class F {public Function showage ($age) {return $age + 100;}} Call_user_func (__namespace__. ' \f::showage ', 23);

Note here that if this is called, the system will error, prompting

Call_user_func () expects parameter 1 to am a valid callback, Non-static method Foo\f::showage () should not being called S
Tatically

Explain this: the first parameter of this function must be a valid callback function, non-static method Showage () is not allowed to be called.

The solution you need is:

Namespace Foo;class F {public static function ShowName ($name) {return strtoupper ($name);} Public Function Showage ($age) {return $age + 100;}} $f = new F (); echo call_user_func (Array ($f, ' showage '), 23);


For calls of such dynamic functions, object instantiation must be done in advance,

The instantiated object is then passed into the function as the first parameter.


Call_user_func_array:

Namespace Foo;class F {public static function ShowName ($name) {return strtoupper ($name);} Public Function Showage ($age) {return $age + 100;}} $f = new F (); echo Call_user_func_array (Array ($f, ' showage '), Array (23));

This is also possible.


To summarize: Call_user_func_array and call_user_func Two functions are basically similar, but there are some differences when passing parameters on a call.

Remember that the second argument passed by Call_user_func_array must be an array,

The second argument passed by the Call_user_func may be an array. If you have more than one parameter, you still need to list it in the form of a listing.



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.