The php tutorial plug in mechanism implements the scheme in PHP

  • 2020-05-26 07:56:53
  • OfStack

Tip: an implementation of the plug-in mechanism in PHP.

The starting point for this article is my understanding of the plug-in mechanism and its implementation in PHP. This scheme is only the implementation of the plug-in mechanism in PHP scheme 1, write down and share with you, welcome to discuss 1.

A plug-in, also known as Plug-in, is a class of 1 specific functional modules (usually implemented by third-party developers) that are activated when you need them and disabled/removed when you don't; In other words, the plug-in is a kind of non-intrusive modular design, which realizes the loose coupling between the core program and the plug-in program. A typical example is the numerous 3rd party plug-ins in Wordpress, such as Akimet for Spam filtering of user comments.

A robust plug-in mechanism, I think, must have the following characteristics:

Dynamic listening and loading of plug-ins (Lookup)
Dynamic triggering of the plug-in

The implementation of the above two points does not affect the operation of the core program

To implement a plug-in in your application, the first thing you should think about is defining different hooks (Hooks). "Hook" is a very visual logical concept, you can think of it as the system reserved plug-in trigger conditions. The logic is as follows: when a hook is executed, the system determines whether the hook condition is satisfied. If so, it will instead call the function specified by the hook and then return to continue executing the rest of the program. If not, skip it. This is a bit like "interrupt protection" logic in assembly.

Some hooks may be pre-designed by the system, such as the hook I mentioned earlier about comment Spam filtering, which is usually designed by the core system developer into the comment processing logic. Another type of hook may be customized by the user (by a third party developer) and usually exists in the presentation layer, such as a normal PHP form display page.

You may find the above words boring and lethargic. But to understand the code I wrote below, understanding the above principles is essential.

The following is the core implementation of the plug-in mechanism in PHP. The whole mechanism core is divided into three parts:

1 plug-in manager class: this is the core of the core. It is an application global Global object. It has three main functions:

It is responsible for listening to all registered plug-ins and instantiating the plug-in objects.
Responsible for registering all plug-ins.
When the hook condition is met, the corresponding object method is triggered.

Plug-in functionality: this is mostly done by third party developers, but it follows a set of rules that are defined by the plug-in mechanism, which varies from one plug-in mechanism to another, as you will see in the display code below.

Plug-in trigger: the trigger condition of the hook. Specifically, this is a small piece of code placed where you need the plug-in implementation to trigger the hook.

Related articles: