Introduction to the method of implementing plug-in mechanism in PHP

Source: Internet
Author: User
This article introduces the content of my understanding of the plug-in mechanism, and its implementation in PHP, this scheme is only one of the implementation of the plug-in mechanism, for reference only.

This article introduces the content of my understanding of the plug-in mechanism, and its implementation in PHP, this scheme is only one of the implementation of the plug-in mechanism, for reference only.

A plug-in, or plug-in, refers to a specific type of function module (usually implemented by a third-party developer), which is characterized by activating it when you need it, disabling/deleting it when it is not needed, and whether activation or disabling does not affect the operation of the system core module. In other words, the plug-in is a non-intrusive modular design, which realizes the loose coupling between the core program and the plug-in program. A typical example is the numerous third-party plugins in WordPress, such as the Akimet plugin for spam filtering of user comments.

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

Dynamic triggering of plug-ins for dynamic listening and loading (Lookup) plugins

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

To implement plug-ins in a program, the first thing we should think of is to define different hooks (Hooks); "Hooks" is a very graphic logical concept that you can think of as a plugin trigger condition reserved by the system. Its logical principle is as follows: When the system executes to a hook, it will determine whether the condition of the hook is satisfied, if satisfied, it will go to invoke the function set by the hook, and then return to continue to execute the rest of the program, if not satisfied, skip. This is a bit like the "interrupt protection" logic in the assembly.

Some hooks may have been designed in advance, such as the one I've previously raised about commenting on spam filtering, usually it has been designed by core system developers into the processing logic of comments, while another type of hook may be customized by the user (developed by a third-party developer), usually in the presentation layer, For example, a normal PHP form is displayed on the page. Please note that the following instructions are understood.

The following is the core implementation of the plug-in mechanism in PHP, the core of the mechanism is divided into three chunks:

A plugin manager class: This is the core of the core. It is an application global object. It has three main responsibilities:

is responsible for listening to all the plugins that have been registered and instantiating the plug-in objects. Responsible for registering all plugins. 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 need to follow certain rules, this rule is provided by the plug-in mechanism, because the plug-in mechanism is different, the following display code you will see this rule.

Plugin 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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.