Learn php to call functions through strings.
1. call_user_func
- 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
- ?>
Copy code
It is strange to call the method inside the class, actually using array , I don’t know how the developers thought about it. Of course, new is omitted, which is also full of novelty:
- class a {
- function b($c){
- echo $c;
- }
- } // www.jbxue.com
- call_user_func(array("a", "b"),"111");
-
- //Display 111
- ?>
Copy code
2. call_user_func_array
The call_user_func_array function is very similar to call_user_func, except that the parameters are passed in a different way to make the structure of the parameters clearer:
- function a($b, $c){
- echo $b;
- echo $ c;
- }
- call_user_func_array('a', array("111", "222"));
-
- //Display 111 222
- ?>
Copy code
The call_user_func_array function can also be called inside the class The method of
- Class ClassA{
- function bc($b, $c) {
- $bc = $b + $c;
- echo $bc;
- }
- } // www.jbxue.com
- call_user_func_array(array('ClassA','bc'), array("111", "222"));
-
- //Display 333
- ?>
Copy code
Both the call_user_func function and the call_user_func_array function are supported quotes, which makes them more functionally consistent with ordinary 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
Copy code
|