Home >Backend Development >PHP Tutorial >Implementation and understanding of hook functions in PHP

Implementation and understanding of hook functions in PHP

巴扎黑
巴扎黑Original
2016-11-11 13:19:561822browse

Suppose there is such a program:

function fun(){

fun1();

fun2();

}

  First, the program executes fun1(), then executes fun2(), and then fun() ends.

  However, suppose we want to make some changes to the function. For example, fun is an analytical function. We hope to provide rich analytical functions in the future. As for which function to use for analysis, we hope to configure it in the configuration file. This is when you can unleash the power of the hook.

  We can add a hook point H in function fun(){}, and then configure the hook function before executing the function H, so that we can parse it as needed.

For example:

$h=config_item("parser_fun");//Get the corresponding configuration information from the configuration file

function fun($data){

global $h;

return $h();

}

  In addition to this, PHP can also provide its own class based on strings, and then call a method of the class to pass certain parameters. These lay a solid foundation for the writing of PHP programs and later maintenance and expansion. Foundation.

$c=get_class_name();//Get the name of the class

$m=get_method_name();//Get the name of the method

$k=$c->$m();//Execute something of the class A method

Original link: Hook function & implementation in PHP

1. The hook function is preset and triggered under specific conditions.

2. After the hook function takes over the program, it can affect the direction of the program.

  The complete implementation of hooks should be called event-driven. Event-driven is divided into two stages. The first stage is to register the event. The purpose is to give a name to the "event" that may occur in the future. The simple implementation method is to use the singleton mode to generate a persistent object or register a global variable. Then insert the event name, as well as the class and method corresponding to the event into the global variable. That is to mount a hook.

 The second stage is to trigger the event, which is essentially to query the event global variable for the name of the event to be triggered, then find the registered class and method, instantiate and run. In this way, you can get rid of the traditional rules that programs must be in order, and further achieve the purpose of decoupling.

 Hook functions can intercept and process messages from other applications. Whenever a specific message is sent, the hook program first captures the message before reaching the destination window, that is, the hook function first obtains control. At this time, the hook function can process (change) the message, continue to deliver the message without processing it, or force the message delivery to end.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php arrayNext article:php array