I am developing a PHP system recently. In order to improve the scalability of the system, I want to add an event processing mechanism similar to Javascript to the system. For example: I want to add a message to the system. After being added, I want to record the log. Using code similar to Javascript, it should be written like this:
function fnCallBack( $news ) { //将$news的信息记录到日志中 writeLog( $news->getTitle().' has been added successfully!'); } $newsEventManager->addEventListener( 'add' , fnCallBack );
Among them, the fnCallBack function is the callback function, and addEventListener means listening for the add event of newsEventManager. . When a news article is added, the system will call the fnCallBack function to complete the writeLog action.
However, the function transfer method in PHP is very different from that in Javascript. In Javascript, functions are also objects, and they can be easily passed as parameters, but not in PHP.
$newsEventManager->addEventListener( 'add' , fnCallBack );
The fnCallBack in the above line of code looks like the handle of that function, but in fact it is a string and not the function we want.
In order to implement our event model, it is necessary to study the implementation method of PHP's callback function.
1. Callback of global function
The global function here means a function defined directly using function. It is not included 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, indicating the name of the function to be called. The second parameter is an array, indicating the parameter list, which will be passed to the function to be called in order.
2. Callback of static methods of a class
What should we do if the method we want to callback is a static method of a class? ? We can still use PHP's built-in call_user_func_array method to make calls. Please see the example:
Sample code:
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. Similarly, if we pass the class name (MyClass) as the first parameter of call_user_func_array, we can implement the callback of the static method of the class. 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.
3. Callback of the object's method
First try the calling method in the most original string form, as shown below:
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();
The call is successful, but how to pass the parameter params to this method? If the params are passed directly Pass it in, then it will be used as a parameter. How to split the params and pass it in?
I checked PHP manual and found the create_function function. This method can use strings to create an anonymous function. OK, I have an idea. I can create an anonymous function. , in this anonymous function, call our callback function and pass the parameters in.
First manually create an anonymous function anonymous. In this function, use the method I learned earlier to call the callback function, as shown below:
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' ); function anonymous() { global $myobj; global $fnName; global $params; $myobj->$fnName( $params[0] , $params[1] ); } anonymous();
Then, I use create_function to create This anonymous function, at the same time, params[0], params[1] in the code should be dynamically generated, the code is as follows:
$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();
This code can define an anonymous function and save it in $ In the anonymous variable, $anonymous is finally called to implement the method callback.
The above is the detailed content of Detailed explanation of the usage of callback function of PHP custom function. For more information, please follow other related articles on the PHP Chinese website!