What is the difference between calling a function back and forth using call_user_func_array () and directly using a function?

Source: Internet
Author: User
I haven't found any big difference after half a day, maybe because I haven't found a scenario that shows the advantages of using the call_user_func_array () function. Who knows. Link to an introduction Article ~ {Code ...} for details, refer to the big one on the first floor. It's really good: {code ...} I haven't found any big difference after half a day, maybe because I haven't found a scenario that shows the advantages of using the call_user_func_array () function.
Who knows.
Link to an introduction Article ~

function foo($a,$b){    return 2*(intval($a)+intval($b));}function bar($a,$b){    return 3*foo($a,$b);}echo bar(3,2);function foo($a,$b){    return 2*(intval($a)+intval($b));}function bar(){    return 3*call_user_func_array('foo', func_get_args());}echo bar(3,2);

It is really good to refer to the big ones on the first floor:

Function playVideo ($ video) {echo 'Brother is using Video to watch movies-', $ video;} function playAudio ($ artist, $ music) {echo 'Brother is using Mp3 to listen to audience ', $ artist, 'songs-', $ music;} function play () {$ args = func_get_args (); $ functionName = array_shift ($ args); call_user_func_array ('play '. $ functionName, $ args);} play ('video', 'Kung Fu '); echo'
'; Play ('audio', 'Jay', 'simple love');

Reply content:

I haven't found any big difference after half a day, maybe because I haven't found a scenario that shows the advantages of using the call_user_func_array () function.
Who knows.
Link to an introduction Article ~

function foo($a,$b){    return 2*(intval($a)+intval($b));}function bar($a,$b){    return 3*foo($a,$b);}echo bar(3,2);function foo($a,$b){    return 2*(intval($a)+intval($b));}function bar(){    return 3*call_user_func_array('foo', func_get_args());}echo bar(3,2);

It is really good to refer to the big ones on the first floor:

Function playVideo ($ video) {echo 'Brother is using Video to watch movies-', $ video;} function playAudio ($ artist, $ music) {echo 'Brother is using Mp3 to listen to audience ', $ artist, 'songs-', $ music;} function play () {$ args = func_get_args (); $ functionName = array_shift ($ args); call_user_func_array ('play '. $ functionName, $ args);} play ('video', 'Kung Fu '); echo'
'; Play ('audio', 'Jay', 'simple love');

I usually seldom use this function, but it still works when the static method of the class is dynamically called:

class foo{    public static function func()     {        var_dump(func_get_args());    }}call_user_func_array(array('foo', 'func'), array('hello world'));

When calling a common function dynamically, it is also helpful in this case:

Function playVideo ($ type, $ src) {echo 'I will watch '. $ src;} function playAudio ($ type, $ src, $ artist) {echo 'I will listen '. $ artist. '\'s '. $ src;} function play () {$ args = func_get_args (); call_user_func_array ('play '. $ args [0], $ args);} play ('video', 'Kung Fu. rmvb '); echo "and" audio play('audio', 'simple love', 'Jay ');

I believe that you have understood its role. In addition,call_user_func()Andcall_user_func_array()Similarly, the second parameter of the latter is an array.

Many scenarios are as follows:
1. The name of the function you want to call is unknown.
2. The parameter type and number of functions to be called are also unknown.
Because it is unknown, you must use functions such as call_user_func_array.

One Application Scenario:
If the scheduled task class needs to run a function regularly, the interface can be designed like this:

/*** Run $ call_back */function task_add ($ run_time, $ call_back, $ param_arr) {while (1) {if (time ()> = $ run_time) {call_user_func_array ($ call_back, $ param_arr); return ;} sleep (1 );}}

$ Call_back and $ param_arr are user-passed and can be any function parameter. In this scenario, it is difficult to directly call a function. It is very convenient to use 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.