In-depth explanation of the principles of PHP plug-ins

Source: Internet
Author: User

In this article, we mainly talk about some

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 PHP 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 PHP plug-ins does not affect the running of core programs.

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 principles of the above PHP plug-in mechanism.

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.

The principles of the PHP plug-in mechanism are described a lot. Let's look at my implementation scheme below:

Plugin manager PluginManager class:

The following content is referenced by the PHP plug-in mechanism:

  1. <?
  2. Class PluginManager
  3. {
  4. Private $ _ listeners = array ();
  5. Public function _ construct ()
  6. {
  7. # Here, the $ plugin array contains the information that we obtain from the user
    Activated plug-in information
  8. # For ease of demonstration, we assume that $ plugin contains at least
  9. # $ Plugin = array (
  10. # 'Name' =>'Plug-In name ',
  11. # 'Directory' =>'Plugin installation directory'
  12. #);
  13. $ Plugins = get_active_plugins ();
    # Implement this function on your own
  14. If ($ plugins)
  15. {
  16. Foreach ($ plugins as $ plugin)
  17. {// Assume that each plug-in folder contains an actions.
    PHP file, which is the specific implementation of the plug-in
  18. If (@ file_exists (STPATH. 'ins INS /'.
    $ Plugin ['Directory '].'/actions. php '))
  19. {
  20. Include_once (STPATH. 'ins INS /'.
    $ Plugin ['Directory '].'/actions. php ');
  21. $ Class = $ plugin ['name']. '_ actions ';
  22. If (class_exists ($ class ))
  23. {
  24. // Initialize all plug-ins
  25. New $ class ($ this );
  26. }
  27. }
  28. }
  29. }
  30. # Here are some log records
  31. }
  32. Function register ($ hook, & $ reference,
    $ Method)
  33. {
  34. // Obtain the implementation method of the plug-in
  35. $ Key = get_class ($ reference ).'->'. $ Method;
  36. // Push the reference of the plug-in along with the method into the listening array
  37. $ This->_ Listeners [$ hook] [$ key] =
    Array (& $ reference, $ method );
  38. # Here are some log records
  39. }
  40. Function trigger ($ hook, $ data = '')
  41. {
  42. $ Result = '';
  43. // Check whether the hook to be implemented is in the listener Array
  44. If (isset ($ this->_ Listeners [$ hook])
    & Is_array ($ this->_ Listeners [$ hook])
    & Count ($ this->_ Listeners [$ hook])>0)
  45. {
  46. // Start of loop call
  47. Foreach ($ this->_ Listeners [$ hook] as $ listener)
  48. {
  49. // Retrieve the reference and method of the plug-in object
  50. $ Class = & $ listener [0];
  51. $ Method = $ listener [1];
  52. If (method_exists ($ class, $ method ))
  53. {
  54. // Dynamically call the plug-in method
  55. $ Result. = $ class->$ Method ($ data );
  56. }
  57. }
  58. }
  59. # Here are some log records
  60. Return $ result;
  61. }
  62. }
  63. ?> 

The core of the entire plug-in mechanism is completed when no more than 100 lines of comments are added to the Code above. It must be noted that you must set it to a global class and load the plug-in first. The # annotation is the part you need to complete on your own, including obtaining the PHP plug-in mechanism and logging.


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.