PHP function call

Source: Internet
Author: User
Tags php class

  below to share with you the use of this call_user_func_array and Call_user_func functions, plus the Func_get_args () function and Func_num_args () function, hey!!  

The Call_user_func function is used when a function needs to be called dynamically, and this function has two uses: the first is to invoke the lonely function: The     code is as follows: <?php function Funa ($b, $c) {    Echo $b;     Echo $c; } call_user_func (' Funa ', "111", "222"); Call_user_func (' Funa ', "333", "444"); Show 111 222 333 444//Have you noticed that this usage is a bit like the call method in JavaScript, hehe?> the second is to invoke a function inside the class: The   code is as follows: <?php Class A {  &NBSP ; Function B ()     {        $args = Func_get_args ();         $num = func_ Num_args ();         Print_r ($args);         echo $num;    } call_user_func (Array ("A", "B"), "a", "222");?> the above example, run it yourself to see what the result is ~ Hey, hint Func_get_args () function is to get the arguments passed into the function, return an array, and the Func_num_args () function gets the number of arguments in the passed-in function.   Let's take a look at the Call_user_func_array function, which is also used when you need to call a function dynamically, and its usage is more like the Call_user_func function, except that the arguments pass in an array.     Code as follows: <?php function A ($b, $c) {    echo $b;     echo $c; } Call_user_func_array (' A ', Array ("111", "222")); Show 222?>The   Call_user_func_array function can also invoke the   code of the method inside the class: <?php class ClassA {    function BC ($b, $c) {  &N Bsp       $BC = $b + $c;         echo $BC;    }  } call_user_func_array (Array (' ClassA ', ' BC '), Array ("111″," 222″)); Show 333?>   below to see an example of a dynamic call function: The   code is as follows: <?php function Otest1 ($a) {     echo (' one parameter '); &N Bsp function Otest2 ($a, $b) {    echo (' two parameters ')}   function Otest3 ($a, $b, $c) {    echo (' three) ' ); }   function Otest () {    $args = Func_get_args ()     $num = Func_num_args ();     CAL L_user_func_array (' otest '. $num, $args  ); } otest ("11"); Otest ("11", "22"); Otest ("One", "All", "?>");  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.