A brief analysis of the call user func () function in PHP and how to invoke a custom function using called user func, callfunc_php tutorial

Source: Internet
Author: User

A brief analysis of the call user func () function in PHP and how to invoke a custom function using called user func, Callfunc


Ucenter source code has a function Call_user_func, began to think that it is a function of their own, the results can not be found everywhere. Later I learned that Call_user_func is a built-in function of PHP that allows the user to call directly written functions and pass in certain parameters, the following summarizes the use of this function.

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

<?phpfunction nowamagic ($a, $b) {  echo $a;  Echo $b; } call_user_func (' Nowamagic ', "", ""); Call_user_func (' Nowamagic ', "", ""); Show  ?>

Call the method inside the class is rather strange, incredibly using an array, do not know how developers are thinking, of course, save the new, but also quite innovative:

<?phpclass a {  function B ($c)  {   echo $c;  

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:

<?phpfunction A ($b, $c) {  echo $b;  

The Call_user_func_array function can also invoke methods within the class:

<?phpclass ClassA {function BC ($b, $c) {   

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:

<?phpfunction A ($b) {  $b + +;} $c =; Call_user_func (' A ', $c); echo $c;//Display Call_user_func_array (' A ', Array ($c)); EC Ho $c;//Display?> In addition, both the Call_user_func function and the Call_user_func_array function support references. View Sourceprint?<?phpfunction Increment (& $var) {$var + +;} $a =; Call_user_func (' increment ', $a); Echo $a; Call_user_func_array (' Increment ', array (& $a)); Can use this insteadecho $a; ?>

Here is a description of using Call_user_func to invoke a custom function

Using the Call_user_func function, you can invoke a custom function by passing in a string function, and support references.

1.mixed Call_user_func (Callable $callback [, Mixed $parameter [, mixed $ ...])

Call the custom function supplied by the first argument, followed by the parameter for the custom function, returning the result of the custom function

function Say ($word) {echo $word;} Call_user_func (' say ', ' Hello World '); Hello world can also call methods in the class: class A {function say ($word = ') {  echo $word;}} $a = new A ();//Note that it must be instantiated unless it is staticcal L_user_func (Array ($a, ' say '), ' Hello World '); Hello World

2.mixed Call_user_func_array (callable $callback, array $param _arr)

Words like Call_user_func_array and call_user_func functions, just a little bit different when calling parameters:

function A ($word) {echo $word;} Class A {function say ($word = ') {  echo $word;}} Call_user_func_array (' A ', array (' Hello World ')); Hello world$a = new A (); Note that it must be instantiated unless it is Staticcall_user_func_array (array ($a, ' say '), array (' Hello World '); Hello World

Note: Another similar two functions are Call_user_method and Call_user_method_array () but after the PHP4.1, they are discontinued.

http://www.bkjia.com/PHPjc/1068830.html www.bkjia.com true http://www.bkjia.com/PHPjc/1068830.html techarticle A brief analysis of the call user func () function in PHP and how to invoke a custom function using the calling user Func, Callfunc ucenter has a function call_user_func in the source code, and begins to assume that it is a function of its own definition ...

  • 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.