Php Tutorial plug-in mechanism implementation solution in PHP. Tip: an implementation scheme of the plug-in mechanism in PHP. the starting point of this article is my understanding of the plug-in mechanism and its implementation in PHP. This solution is only the implementation scheme of the plug-in mechanism in PHP. note: This is an implementation scheme of the plug-in mechanism in PHP.
The starting point of this article is my understanding of the plug-in mechanism and its implementation in PHP. This solution is only one of the implementation solutions of the plug-in mechanism in PHP. write it down and share it with you. you are welcome to discuss it together.
Plug-ins, that is, Plug-in, refer to a specific type of functional modules (usually implemented by third-party developers). they are characteristic of activating a Plug-in when you need it, disable/delete a plug-in when you do not need it. whether it is activated or disabled, it does not affect the operation of the system's core modules. that is to say, the plug-in is a non-intrusive modular design, loose coupling between the core program and plug-in program is realized. A typical example is many third-party plug-ins in Wordpress. for example, the Akimet plug-in is used to filter comments by Spam.
I think a robust plug-in mechanism must have the following features:
Plug-in dynamic listening and loading (Lookup)
Dynamic triggering of plug-ins
The implementation of the above two points does not affect the running of the core program.
To implement plug-ins in a program, we should first think of defining different Hooks. "Hooks" are a very visual logical concept, you can think of it as a trigger condition for system-reserved plug-ins. Its logic principle is as follows: when the system executes a hook, it will determine whether the hook's conditions are met; if yes, it will call the hook's functions first, then return to continue to execute the remaining program; if not, Skip. This is a bit like the interrupt protection logic in assembly.
Some hooks may have been designed in advance by the system. for example, the hooks I mentioned earlier for commenting on Spam filtering are usually designed by core system developers into the comment processing logic; another type of hooks may be customized by users (developed by third-party developers) and usually exist in the presentation layer, such as a common PHP form display page.
You may feel bored and sleepy. but to understand the code I wrote below, it is essential to understand the above principles.
The following describes the core implementation of the plug-in mechanism in PHP. The core of the entire mechanism is divided into three parts:
A plug-in manager class: this is the core. It is a Global object of an application. It has three main responsibilities:
Monitors all registered plug-ins and instantiates these plug-ins.
Registers all plug-ins.
When the hook condition is met, the corresponding object method is triggered.
Plug-in function implementation: this is mostly done by third-party developers, but certain rules need to be followed. this rule is defined by the plug-in mechanism and varies depending on the plug-in mechanism, the following code shows the rule.
Plug-In trigger: the trigger condition of the hook. Specifically, this is a small piece of code, which is placed where you need to implement the plug-in to trigger this hook.
Summary. the starting point of this article is my understanding of the plug-in mechanism and its implementation in PHP. This solution is only the implementation scheme of the plug-in mechanism in PHP...