Cause of php event processing mechanism (callback function)
Js has an event processing mechanism. When an event is triggered, the set callback function is called.
For example
$ ("# P"). on ('click', function () {// code segment });
Can php be the best language in the world?
The answer is yes, such as swoole.
$ Serv = new swoole_server ("127.0.0.1", 9501); $ serv-> set (array ('worker _ num' => 8, // number of worker processes 'daemonize' => true, // whether it is used as a daemon); $ serv-> on ('connection', function ($ serv, $ fd) {echo "Client: Connect. \ n ";}); $ serv-> on ('receive ', function ($ serv, $ fd, $ from_id, $ data) {$ serv-> send ($ fd, 'swoole :'. $ data); $ serv-> close ($ fd) ;}); $ serv-> on ('close', function ($ serv, $ fd) {echo "Client: close. \ n ";}); $ serv-> start ();
Another example is Phalcon's reset api.
$app = new Phalcon\Mvc\Micro();//Retrieves all robots$app->get('/api/robots', function() {});//Searches for robots with $name in their name$app->get('/api/robots/search/{name}', function($name) {});
And so on
Start
This kind of handsome use of php as js is impossible.
As the saying goes, you cannot decide to ask Baidu or Gu Ge for foreign affairs,
Then du Niang told me the blog of this great God.
PHP callback function implementation method
After talking about the principle, I thought about something like this,
Class MyClass {public $ eventMap = array (); function on ($ evtname, $ handle) {// register the response callback function on an event $ this-> eventMap [$ evtname] = $ handle;} function trigger ($ evtname, $ scope = null) {// trigger an event, that is, call all the callback functions call_user_func_array ($ this-> eventMap [$ evtname], $ scope) that respond to this event cyclically );}}
This is an official description of call_user_func_array.
In his blog, The Great God talked about the call methods and parameter passing methods for three types of functions. in order to be able to use object attributes, he mentioned that using create_function to create a function is unnecessary, if an object's method is called and the object's attributes are used in the method, pass the parameter directly to the object.
This is the case.
$ Func = array ('class name', 'class method name'); $ args = array ('Parameter 1', 'parameter 2'); call_user_func_array ($ func, $ args );
$ Func is a string. an error is returned when $ this is included in the call method,
So I suggest this.
$ Func = array ($ object, 'method name' of the class'); $ args = array ('Parameter 1', 'parameter 2'); call_user_func_array ($ func, $ args );
So there is no problem.
Next let's take a look at the effect
Let's start with a public
$ MyClass = new MyClass; $ MyClass-> on ('post', function ($ a, $ B) {echo "a = $ a; \ n "; echo "B = $ B; \ n"; echo "a + B = ". ($ a + $ B ). "; \ r \ n" ;}); $ MyClass-> trigger ('post', array (123,321); // triggered inside the framework
For example
We hope to call different functions for processing based on different http requests;
For example, if a post request is received, the Register function corresponding to post is called.
Then, the method of changing the class
class test{ static $static = "this is static "; public $nomal = "this is nomal "; function demo($a , $b ){ echo " a = $a ;\r\n"; echo " b = $b ;\r\n"; echo " static = ".self::$static." ;\r\n"; echo " nomal = ".$this->nomal." ;\r\n"; echo " add = ".$this->add." ;\r\n"; }}
Then come
$test = new test;$test->add = " this is new add ";$MyClass->on('post' ,array( $test , 'demo' ) );$MyClass->trigger('post' , array( 123 , 321 ) );
OK, no problem.
In fact, I think of another thing,
There is an array of objects $ objects. We bind each object with a callback scheme for their respective events and events,
For example, when a thief comes, we want to beat 110; when a friend comes, we want to treat dinner.
I don't know if there is any need for such a scenario in the actual project, and I haven't thought about how to deal with it for the moment.