PHP functions Call_user_func and Call_user_func_array usage examples

Source: Internet
Author: User
See Ucenter when there is a function call_user_func, think not its solution, because I thought it is the function of their own definition, the results can not be found everywhere, and then Baidu a bit to know Call_user_func is built-in function

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

function A ($b, $c) {echo $b; echo $c;} call_user_func (' A ', "111", "222"); Call_user_func (' A ', "333", "444"); Display 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:

Class A {function B ($c) {echo $c;}} call_user_func (Array ("A", "B"), "111"); Showing 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:

function A ($b, $c) {echo $b; echo $c;} call_user_func_array (' A ', Array ("111", "222")); Showing 111 of 222

The Call_user_func_array function can also invoke methods within the class.

Class ClassA {function BC ($b, $c) {$BC = $b + $c; echo $bc;}} call_user_func_array (Array (' ClassA ', ' BC '), Array ("111", "222")); Showing 333

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:

Function A (& $b) {$b + +;} $c = 0; Call_user_func (' A ', & $c); echo $c;//Display 1 Call_user_func_array (' A ', Array (& $c)); echo $c;//Display 2

Simple usage of PHP's Call_user_func_array

<?php function Foobar ($arg, $arg 2) {echo function, got $arg and $arg 2\n ";} class Foo {function bar ($arg, $arg 2) { Echo METHOD, "Got $arg and $arg 2\n"; }}//Call the Foobar () function with 2 arguments call_user_func_array ("Foobar", Array ("One", "one")); Call the $foo->bar () method with 2 arguments $foo = new Foo; Call_user_func_array (Array ($foo, "bar"), Array ("Three", "four"),?>

The output of the above routines is similar to the following:
Foobar got one and
Foo::bar got three and four
Example #2 Call_user_func_array () using namespace name

<?php namespace Foobar; Class Foo {static public function test ($name) {print "Hello {$name}!\n"}}//As of PHP 5.3.0 Call_user_func_array (NAM ESPACE. ' \foo::test ', Array (' Hannes ')); As of PHP 5.3.0 Call_user_func_array (Array (NAMESPACE. ' \foo ', ' Test '), Array (' Philip ');?>

The output of the above routines is similar to the following:
Hello hannes!
Hello philip!
Example #3 Using lambda function

<?php $func = function ($arg 1, $arg 2) {return $arg 1 * $arg 2;}; Var_dump (Call_user_func_array ($func, Array (2, 4))); /* As of PHP 5.3.0 */?>

The above routines will output:
Int (8)

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.