Implementation and recognition of hook function in PHP Category: PHP programmingrming time: 2014-09-21
If there is such a procedure:
function fun(){
fun1();
fun2();
}
First the program executes FUN1 () after executing fun2 () and then fun () ends.
But, if we want to make some changes to the function. For example, fun is an analytic function, we hope that later can provide a rich analytic function, and which function to parse, we want to configure in the configuration file. The power of the hook can be played at this time.
We can put a hook point h in function Fun () {} and then execute the function of H, and then configure the hook function so that I can parse it as needed.
For example:
$h=config_item("parser_fun") ;//从配置文件中获得相应的配置信息
function fun($data){
global $h;
return $h();
}
In addition to this accident, PHP can also provide its own class based on the string, and then invoke a method of the class, passing some parameters, which for the PHP program writing, as well as the later maintenance extension laid a fairly solid foundation.
$c=get_class_name(); //获得类的名字
$m=get_method_name(); //获得方法的名字
$k=$c->$m(); //执行类的某一个方法
SOURCE Link: Php hook function & Implementation
- 1, the hook function is preset and triggered under certain conditions.
- 2, hook function takeover program can affect the direction of the program.
The complete implementation of the hooks should be called event-driven. The event driver is divided into two stages, the first stage is the registration event, the purpose is to give a name for future "events", the simple implementation is to use a singleton pattern to produce a persistent object or register a global variable, and then the event name, and the event corresponding class and method to insert global variables. That is, to mount a hook.
The second stage is the trigger event, which essentially queries the event name to be triggered in the global variables of the event, and then finds the registered class and method, instantiates and runs. This way you can get rid of the traditional way the program must follow the rules in order to further achieve the purpose of decoupling.
The hook function can intercept and process messages from other applications. Whenever a particular message is issued, the hook program captures the message before it reaches the destination window, i.e. the hook function gets control first. At this point the hook function can process (change) the message, or you can continue to pass the message without processing, and you can force the end of the message delivery.
- Diary 20140726
- Generate a small number of non-repeating random numbers within a specified range
Implementation and cognition of hook function in PHP