Call_user_func of PHP functions & Call_user_func_array

Source: Internet
Author: User

Call_user_func_array (callable $callback, array $param _arr)

Number 1: Call a callback function,

Parameter 2: Array parameters are the parameters of the callback function.


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

Parameter 1: callback function for Call

2-n: The number of parameters for the callback function.


The difference between the two is different oh. The second parameter 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 difference between the two.

Look out please continue.


Scenario Two: Call a static function in the class.

Here are the 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 are the 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: The first parameter of this function must be a valid callback function, non-static method Showage () is not agreed to call.

The required solutions are:

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 the invocation of this dynamic function, object instantiation must be done in advance,

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


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: The two functions of Call_user_func_array and Call_user_func are basically similar, but there are some differences when passing parameters on a call.

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

The second parameter passed by the Call_user_func may be an array. If you have multiple parameters, you need to list them.



Call_user_func &amp; Call_user_func_array of PHP functions

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.