Global callback function:
The meaning of the global function here is to directly use functions defined by the function, which is not contained in any object or class. Take a look at the example below.
Example code:
function Fncallback ($msg 1, $msg 2) { echo ' MSG1: '. $msg 1; echo "<br/>n"; Echo ' MSG2: '. $msg 2;} $fnName = "Fncallback"; $params = array (' Hello ', ' world '); Call_user_func_array ($fnName, $params);
Code Description:
The PHP built-in function Call_user_func_array is used here to make the call. Call_user_func_array has two parameters, the 1th parameter is a string representing the function name to invoke, and the 2nd parameter is an array representing the list of parameters, which in turn is passed to the function to be called.
The effect is as follows:
Callback for the static method of the class:
What if we want the callback method, which is a static method of a class? We can still use PHP's built-in Call_user_func_array method to make calls, see example:
Class myclass{public static function Fncallback ($msg 1, $msg 2) { echo ' MSG1: '. $msg 1; echo "<br/>n"; Echo ' MSG2: '. $msg 2; }} $className = ' MyClass '; $fnName = "Fncallback"; $params = array (' Hello ', ' world '); Call_user_func_array (Array ($className , $fnName), $params);
Code Description:
This code is similar to the code of the 1th method, and we can implement the callback of the class's static method by passing the class name (MyClass) as the 1th parameter of the Call_user_func_array. Note that at this point the 1th parameter of Call_user_func_array is an array, the 1th element of the array is the class name, the second element is the name of the function to be called
Operation Result:
(In fact, the result of the 1th method is the same ^_^)
Continue to study
What happens if I call a non-static method of a class in this way (that is, remove static)? Take a look at the following code
Class myclass{public function Fncallback ($msg 1, $msg 2) { echo ' MSG1: '. $msg 1; echo "<br/>n"; Echo ' MSG2: '. $msg 2; }} $className = ' MyClass '; $fnName = "Fncallback"; $params = array (' Hello ', ' world '); Call_user_func_array (Array ($className , $fnName), $params);
The result is still the same!
Callback for the method of the object:
I tried it first with the most primitive string invocation method, as follows:
Class myclass{ Private $name = ' abc '; Public Function Fncallback ($msg 1 = ' default Msg1 ', $msg 2 = ' default Msg2 ') { echo ' object name: '. $this->name ; echo "<br/>n"; Echo ' MSG1: '. $msg 1; echo "<br/>n"; Echo ' MSG2: '. $msg 2; }} $myobj = new MyClass (); $fnName = "Fncallback"; $params = array (' Hello ', ' world '); $myobj--$fnName ();
Success, output
The call was successful, but how to pass the parameter params to this method, if the params is passed directly into it, then it will be used as 1 parameters, how to take the params apart to pass in it?
Check the PHP manual, found the Create_function function, this method can use a string to create an anonymous function, good, have ideas, you can create an anonymous function, in this anonymous function, call our callback function, and the parameters passed in.
Class myclass{ Private $name = ' abc '; Public Function Fncallback ($msg 1 = ' default Msg1 ', $msg 2 = ' default Msg2 ') { echo ' object name: '. $this->name ; echo "<br/>n"; Echo ' MSG1: '. $msg 1; echo "<br/>n"; Echo ' MSG2: '. $msg 2; }} $myobj = new MyClass (); $fnName = "Fncallback"; $params = array (' Hello ', ' world '); $strParams = '; $strCode = ' global $myobj; global $fnName; global $params; $myobj-$fnName ('; for ($i = 0; $i < count ($params); $i + +) { $strParams. = (' $params ['. $i. '] '); if ($i! = Count ($params)-1) { $strParams. = ', '; }} $strCode = $strCode. $strParams. "); $anonymous = Create_function (", $strCode); $anonymous ();
The realization method of the blog»php callback function of water trivial ' s
PHP callback function Implementation method (GO)