GlobalCallback function:
The global function here means that it is defined directly using function Function, which is not contained in any object or class. Please see the example below.
Sample code:
function fnCallBack( $msg1 , $msg2 ) { echo 'msg1:'.$msg1; echo "<br />n"; echo 'msg2:'.$msg2; } $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 first parameter is a string , which represents the name of the function to be called. The second parameter is an array, which represents the parameter list, which will be passed to the function to be called in order. .
The effect is as follows:
The callback of the static method of the class:
If The method we want to call back is a static method of a class, so what should we do? We can still use PHP's built-in call_user_func_array method to make calls. Please see the example:
class MyClass { public static function fnCallBack( $msg1 , $msg2 ) { echo 'msg1:'.$msg1; echo "<br />n"; echo 'msg2:'.$msg2; } } $className = 'MyClass'; $fnName = "fnCallBack"; $params = array( 'hello' , 'world' ); call_user_func_array( array( $className , $fnName ) , $params );
Code description:
This code is very similar to the code of the first method. We change the class name (MyClass) is also passed in as the first parameter of call_user_func_array, and the callback of the static method of the class can be implemented. Note that the first parameter of call_user_func_array is an array at this time. The first element of the array is the class name, and the second element is the name of the function to be called.
Run result:
(In fact, the result is the same as the first method ^_^)
Continue to study
If I use this method to call a non-static method of a class (that is, remove the static) , what will be the result? Please see the following code
class MyClass { public function fnCallBack( $msg1 , $msg2 ) { echo 'msg1:'.$msg1; echo "<br />n"; echo 'msg2:'.$msg2; } } $className = 'MyClass'; $fnName = "fnCallBack"; $params = array( 'hello' , 'world' ); call_user_func_array( array( $className , $fnName ) , $params );
The running result is still the same as the picture above!
Object method callback:
我先用最原始的字符串形式的调用方法尝试了一下,如下所示:
class MyClass { private $name = 'abc'; public function fnCallBack( $msg1 = 'default msg1' , $msg2 = 'default msg2' ) { echo 'object name:'.$this->name; echo "<br />n"; echo 'msg1:'.$msg1; echo "<br />n"; echo 'msg2:'.$msg2; } } $myobj = new MyClass(); $fnName = "fnCallBack"; $params = array( 'hello' , 'world' ); $myobj->$fnName();
成功了,输出结果
调用是成功了,不过如何把参数params传给这个方法呢,如果把params直接传进去,那么它会作为1个参数,怎么把params拆开来传进去呢?
查了下PHP手册,找到了create_function函数,这个方法可以用字符串来创建一个匿名函数,好,有思路了,可以创建一个匿名的函数,在这个匿名函数中,调用我们的回调函数,并把参数传进去。
class MyClass { private $name = 'abc'; public function fnCallBack( $msg1 = 'default msg1' , $msg2 = 'default msg2' ) { echo 'object name:'.$this->name; echo "<br />n"; echo 'msg1:'.$msg1; echo "<br />n"; echo 'msg2:'.$msg2; } } $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 above is the detailed content of How to implement php callback function?. For more information, please follow other related articles on the PHP Chinese website!