Cause
JS has an event handling mechanism that invokes the set callback function when the event is triggered.
For example
$ ("#div"). On (' click ', Function () { //code snippet});
So, as the best language in the world, can PHP do it?
The answer is yes, like swoole.
$serv = new Swoole_server ("127.0.0.1", 9501), $serv->set (Array ( ' worker_num ' = 8, //number of worker Processes ') Daemonize ' = = true,//whether as daemon); $serv->on (' Connect ', 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
Begin
This kind of PHP when JS with such a handsome thing, my concubine can't do ah.
As the saying goes, ask Baidu, foreign affairs Google Ask,
And then my mother told me the Great God's blog.
How to implement PHP callback function
The great God said the principle, I think I want to write something like this,
Class myclass{public $eventMap = Array (); function on ($evtname, $handle) {//Register response callback function on an event $this->eventmap[$evtname]= $handle; } Function trigger ($evtname, $scope =null) {//triggers an event, that is, the loop invokes all callback functions that respond to this event Call_user_func_array ($this eventmap[$evtname], $scope);} }
There's this thing in there. Call_user_func_array Official website description
The great God in his blog said three types of function call method and the method of the transfer, the blog mentioned in order to be able to use the object's properties, create a function with create_function, which I do not feel necessary, if it is called an object method, which uses the object's properties, It would be nice to pass the parameters directly over the object.
I'll say it again.
$func = Array (' Class name ', ' Method name of class ' ), $args = Array (' parameter 1 ', ' parameter 2 '); Call_user_func_array ($func, $args);
The $func in this is a string, and when there is something $this in the method called, the return is wrong,
Then I suggest it is.
$func = Array ($object, ' method name of class ' ); $args = Array (' parameter 1 ', ' parameter 2 '); Call_user_func_array ($func, $args);
That's fine.
Here's a look at the effect
Let's get a public first.
$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) );//Frame internal trigger
As an example,
We want to call different functions to handle the different requests of HTTP;
For example, when a POST request is received, the registration function corresponding to the post is called.
Then there's a way to change 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 on.
$test = new test; $test->add = "This is new add"; $MyClass->on (' Post ', Array ($test, ' demo ')); $MyClass->trigge R (' Post ', array (123, 321) );
OK, no problem.
I actually thought of something else,
There is an array of objects $objects, we bind each object to its own callback scheme for events and events,
For example, the thief came, we have to call 110, friends come, we have to treat dinner.
I don't know if there is any such situation in the actual project, and I have no idea how to deal with it at the moment.