Is there anything bad about the phpcall_user_func_array function?

Source: Internet
Author: User
Do you understand the following statements? What is the problem with the call_user_func_array function? Why do we need to write so many code that can be called directly? {Code...} directly use the last returncall_user_func_array ($ func, $ params); isn't it enough? Please kindly advise, little... do you understand the following statements? What is the problem with the call_user_func_array function? Why do we need to write so many code that can be called directly?

    public static function invokeMethod($func, array &$params = array()) {        list($class, $method) = $func;        $instance = is_object($class);                switch (count($params)) {            case 0:                return ($instance) ?                    $class->$method() :                    $class::$method();            case 1:                return ($instance) ?                    $class->$method($params[0]) :                    $class::$method($params[0]);            case 2:                return ($instance) ?                    $class->$method($params[0], $params[1]) :                    $class::$method($params[0], $params[1]);            case 3:                return ($instance) ?                    $class->$method($params[0], $params[1], $params[2]) :                    $class::$method($params[0], $params[1], $params[2]);            case 4:                return ($instance) ?                    $class->$method($params[0], $params[1], $params[2], $params[3]) :                    $class::$method($params[0], $params[1], $params[2], $params[3]);            case 5:                return ($instance) ?                    $class->$method($params[0], $params[1], $params[2], $params[3], $params[4]) :                    $class::$method($params[0], $params[1], $params[2], $params[3], $params[4]);            default:                return call_user_func_array($func, $params);        }    }

Use the last return call_user_func_array ($ func, $ params? Please give me more advice. I really don't know much about it.

Reply content:

Do you understand the following statements? What is the problem with the call_user_func_array function? Why do we need to write so many code that can be called directly?

    public static function invokeMethod($func, array &$params = array()) {        list($class, $method) = $func;        $instance = is_object($class);                switch (count($params)) {            case 0:                return ($instance) ?                    $class->$method() :                    $class::$method();            case 1:                return ($instance) ?                    $class->$method($params[0]) :                    $class::$method($params[0]);            case 2:                return ($instance) ?                    $class->$method($params[0], $params[1]) :                    $class::$method($params[0], $params[1]);            case 3:                return ($instance) ?                    $class->$method($params[0], $params[1], $params[2]) :                    $class::$method($params[0], $params[1], $params[2]);            case 4:                return ($instance) ?                    $class->$method($params[0], $params[1], $params[2], $params[3]) :                    $class::$method($params[0], $params[1], $params[2], $params[3]);            case 5:                return ($instance) ?                    $class->$method($params[0], $params[1], $params[2], $params[3], $params[4]) :                    $class::$method($params[0], $params[1], $params[2], $params[3], $params[4]);            default:                return call_user_func_array($func, $params);        }    }

Use the last return call_user_func_array ($ func, $ params? Please give me more advice. I really don't know much about it.

The reason for execution efficiency. In php, use the "variable function "(variiable function) Comparedcall_user_func()Slow, andcall_user_func_array()Ratiocall_user_func()It is also slower.

The related benchmark is as follows:

# Benchmark (2 million iterations)# Operation                    Seconds# Literal function          1.218# Variable function         1.305# call_user_func()          2.734# call_user_func_array()    3.386

As you can see, variable functions and common functions call (literal function) The speed does not differ much.call_user_funcThe difference is more than doubled, whilecall_user_func_arrayIt will be slower. So the code you give will be avoided as much as possiblecall_user_func_arrayBut more inclined to use variable functions. Because only a few functions have more than five parameters, the above Code uses variable functions to write calls to one or five functions, leaving only a small part of functionscall_user_func_arrayTo speed up program execution.

In fact, the efficiency differences between these functions will not be as big as imagined. The above benchmark is the result of 2 million iterations, if your program only calls 'call _ user_func_array 'several thousand times or below, these differences will be very small (about 0.002 seconds), and there is no need to optimize these areas.

Benchmark link:
Http://www.garfieldtech.com/blog/benchmarking-magic

$ Func indicates that the first parameter is the instance and Method Name of the array containing two parameter classes or classes. It is also possible to pass the function name in.

Therefore, except that call_user_func_array is an execution function, all other methods of the execution class or static methods of the class

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.