PHP callback functions Call_user_func and Call_user_func_array

Source: Internet
Author: User

Call_user_func ($fun);

The Call_user_func function is similar to a particular method of calling a function, using the following method:

1. Call the normal function:

<? PHP function A ($b$c) {    echo$b;     Echo $c ;} Call_user_func (' A ', "111", "222"); Call_user_func (' A ', "333", "444"); // Show 111 222 333 444?>

Call the method inside the class is rather strange, incredibly using an array, do not know how developers think, of course, save new, is full of innovative:

2. Methods of invoking classes (including static methods of classes and instance object methods)

<?PHPclassa {functionb$i) {        Echo $i; }     Public StaticC$k) {        Echo $k; }} //when PHP is <5.3, it can be used as follows, and the B () method is considered a static mode of a. Call_user_func(Array("A", "B"), "111"); //when PHP >=5.3, the exposed non-static method of the class must be called before the class is instantiated, otherwise it will prompt for strict errors (in order to be compatible with previous and later versions, or with object methods). $obj=NewA;Call_user_func(Array($obj, "B"), "111");//Show 111//static method can be called as followsCall_user_func(Array("A", "B"), "111");//orCall_user_func("A::b", "111");?>

The Call_user_func_array function is similar to the Call_user_func, except that the parameter is passed in a different way, allowing the structure of the parameter to be clearer:

Call_user_func_array ($fun,$arr);

<? PHP function A ($b$c) {    echo$b;     Echo $c  call_user_func_arrayArray("111", "222")); // Show 111 222?>

The Call_user_func_array function can also invoke methods inside the class, except that the following arguments are passed in as an array.

<?PHPClassclassa{functionbc$b,$c) {        $BC=$b+$c; Echo $BC; }     functiond () {$BC=$b+$c; Echo $BC; }} //php<5.3, non-static methods can be passed directly to the class nameCall_user_func_array(Array(' ClassA ', ' BC '),Array("111", "222")); //php>=5.3, a non-static method can be called only if the class is instantiated, otherwise it will prompt for strict error$obj=NewClassA;Call_user_func_array(Array($obj, ' BC '),Array("111", "222")); //static method calls are as followsCall_user_func_array(Array(' ClassA ', ' BC '),Array("111", "222"));//orCall_user_func_array(' CLASSA::BC ',Array("111", "222"));?>

Both the Call_user_func function and the Call_user_func_array function support references, which makes them more functionally consistent with normal function calls:

<?PHPfunctionA (&$b) {    $b++;} $c= 0;Call_user_func(' A ', &$c);//Note that in the 5.* version, Call_user_func does not advocate reference passing and hints are obsolete. Echo $c;//showing 1Call_user_func_array(' A ',Array(&$c));Echo $c;//showing 2?>

http://php.net/manual/zh/function.call-user-func.php

http://php.net/manual/zh/function.call-user-func-array.php

Ext.: https://www.cnblogs.com/52php/p/5659978.html

http://qianyunlai.com/

PHP callback functions Call_user_func and Call_user_func_array

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.