What is the function of the hook function in thinkphp? What is generally used to process what business logic?
What is the function of the hook function in thinkphp? What is generally used to process what business logic?
A hook is like a mounting point, attached to a function. When this mount point is encountered during function execution, this hook will pull out a piece of code and execute the code. Hook functions are safer and more convenient than calling another function directly within a function. When we need to modify the extended function, we do not need to modify the hook in function B, we only need to modify the code block in the hook. If we directly modify function A, the class where function B is located will be frequently modified. Violates the principle of closure. Another point is that using hooks is more convenient for later maintenance and function expansion.
In thinkphp, hooks are behavior extensions. Here are some explanations of behavior extensions online:
behavior
Behavior is a relatively abstract concept. You can imagine it as an action or process during the execution of the application. In the execution process of the framework, behaviors can be generated at various locations. For example, routing detection is a behavior , Static caching is a behavior, and user permission detection is also a behavior. It can be regarded as a behavior as large as business logic, as small as browser detection, multi-language detection, etc. You can even say that you want to give your website users their first visit. Pop up Hello, world! These can be regarded as a kind of behavior. The existence of behavior allows you to change or add some functions through extension or configuration on the periphery without changing the framework and application.
Different behaviors also have positional similarities. For example, some behaviors take effect before the application is executed, and some actions take effect after the template is output. We call the positions where these behaviors take effect labels (positions). ) (tag), when the application runs to this tag, it will be intercepted and related behaviors will be uniformly executed