How php implements the website plug-in mechanism

Source: Internet
Author: User
These days I want to implement the plug-in function on my website. Google once found an article that seems to be of great help to me. I will post it and share it with you. I will not talk much about it, post code for analysis. the first is the implementation of the plug-in management class:
The code is as follows:
/**
* STBLOG PluginManager Class
*
* Core plug-in implementation class
*
* @ Package STBLOG
* @ Subpackage Libraries
* @ Category Libraries
* @ Author Saturn
* @ Link http://www.cnsaturn.com/
*/
Class PluginManager
{
/**
* Listen to registered plug-ins
*
* @ Access private
* @ Var array
*/
Private $ _ listeners = array ();
/**
* Constructor
*
* @ Access public
* @ Return void
*/
Public function _ construct ()
{
# Here, the $ plugin array contains the plug-in information that has been activated by the user.
# For ease of demonstration, we assume that $ plugin contains at least
# $ Plugin = array (
# 'Name' => 'Plug-In name ',
# 'Directory '=> 'plugin installation directory'
#);
$ Plugins = get_active_plugins (); # implement this function on your own.
If ($ plugins)
{
Foreach ($ plugins as $ plugin)
{// Assume that each plug-in folder contains an actions. php file, which is the specific implementation of the plug-in.
If (@ file_exists (STPATH. 'ins ins/'. $ plugin ['directory']. '/actions. php '))
{
Include_once (STPATH. 'ins ins/'. $ plugin ['directory']. '/actions. php ');
$ Class = $ plugin ['name']. '_ actions ';
If (class_exists ($ class ))
{
// Initialize all plug-ins
New $ class ($ this );
}
}
}
}
# Here are some log records
}

/**
* Register the plug-in method (hook) to be listened)
*
* @ Param string $ hook
* @ Param object $ reference
* @ Param string $ method
*/
Function register ($ hook, & $ reference, $ method)
{
// Obtain the implementation method of the plug-in
$ Key = get_class ($ reference). '->'. $ method;
// Push the reference of the plug-in along with the method into the listening array
$ This-> _ listeners [$ hook] [$ key] = array (& $ reference, $ method );
# Here are some log records
}
/**
* Trigger a hook
*
* @ Param string $ hook name
* @ Param mixed $ input parameter of the data hook
* @ Return mixed
*/
Function trigger ($ hook, $ data = '')
{
$ Result = '';
// Check whether the hook to be implemented is in the listener array
If (isset ($ this-> _ listeners [$ hook]) & is_array ($ this-> _ listeners [$ hook]) & count ($ this-> _ listeners [$ hook])> 0)
{
// Start of loop call
Foreach ($ this-> _ listeners [$ hook] as $ listener)
{
// Retrieve the reference and method of the plug-in object
$ Class = & $ listener [0];
$ Method = $ listener [1];
If (method_exists ($ class, $ method ))
{
// Dynamically call the plug-in method
$ Result. = $ class-> $ method ($ data );
}
}
}
# Here are some log records
Return $ result;
}
}
?>

Then, the specific implementation method of the plug-in is as follows:
The code is as follows:
/**
* This is an implementation of a simple Hello World plug-in.
*
* @ Package DEMO
* @ Subpackage DEMO
* @ Category Plugins
* @ Author Saturn
* @ Link http://www.cnsaturn.com/
*/
/**
* Note the following default rules:
* 1. the file name of this plug-in class must be action.
* 2. the plug-in class name must be {plug-in name_actions}
*/
Class DEMO_actions
{
// The parameters of the parsing function are referenced by pluginManager.
Function _ construct (& $ pluginManager)
{
// Register this plug-in
// The first parameter is the name of the hook.
// The second parameter is the reference of pluginManager.
// The third method is the method executed by the plug-in.
$ PluginManager-> register ('demo', $ this, 'Say _ hello ');
}

Function say_hello ()
{
Echo 'hello world ';
}
}
?>

For example, if you want to put say_hello to Index. php on the homepage of my blog, write it at a location in index. php: (The author's original words)
The code is as follows:
$ PluginManager-> trigger ('demo ','');

The above is the implementation of a plug-in mechanism. over!

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.