Home > Backend Development > PHP Tutorial > How to use sumproduct function How to implement website plug-in mechanism in PHP

How to use sumproduct function How to implement website plug-in mechanism in PHP

WBOY
Release: 2016-07-29 08:41:10
Original
1469 people have browsed it

The first is the implementation of the plug-in management class:

Copy the code The code is as follows:


/**
* STBLOG PluginManager Class
*
* The implementation core class of the plug-in mechanism
*
* @package STBLOG
* @subpackage Libraries
* @category Libraries
* @author Saturn
* @link http://www.cnsaturn.com /
*/
class PluginManager
{
/**
* Monitor 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 we obtain that has been activated by the user
#For the convenience of demonstration, we assume that $ The plugin must contain at least
#$plugin = array(
# 'name' => 'Plug-in name',
# 'directory' => 'Plug-in installation directory'
#);
$plugins = get_active_plugins();# Please implement this function by yourself
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 ​​.'plugins/'.$plugin['directory'].'/actions.php'))
{
include_once(STPATH ​​.'plugins/'.$plugin['directory'].'/actions.php ');
$class = $plugin['name'].'_actions';
if (class_exists($class))
{
//Initialize all plug-ins
new $class($this);
}
}
}
}
#Do some logging stuff here
}
/**
* Register the plug-in method (hook) that needs to be monitored
*
* @param string $hook
* @param object $reference
* @param string $method
*/
function register($hook, &$reference, $method)
{
//Get the method to be implemented by the plug-in
$key = get_class($reference).'->'.$method;
//Push the plug-in reference and the method into the listening array
$this->_listeners[$hook][$key] = array (&$reference, $method);
#Do some logging stuff here
}
/**
* Trigger a hook
*
* @param string $hook The name of the hook
* @param mixed $data The input parameters of the hook
* @return mixed
*/
function trigger($hook, $data='')
{
$result = '';
//Check whether the hook to be implemented is in the listening array
if (isset($this->_listeners[$hook]) && is_array($this->_listeners[$hook]) && count ($this->_listeners[$hook]) > 0)
{
// The loop call starts
foreach ($this->_listeners[$hook] as $listener)
{
// Take out the plug-in object References and methods
$class =& $listener[0];
$method = $listener[1];
if(method_exists($class,$method))
{
// Dynamically call the plug-in method
$result. = $class->$method($data);
}
}
}
#Do some logging stuff here
return $result;
}
}
?>


Then the plug-in Specific implementation method:

Copy the code The code is as follows:


/**
* This is an implementation of a Hello World simple plug-in
*
* @package DEMO
* @subpackage DEMO
* @category Plugins
* @author Saturn
* @link http://www.cnsaturn.com/
*/
/**
*A few default rules to note:
* 1. The file name of this plug-in class must be action
* 2. The name of the plug-in class must be {plugin name_actions}
*/
class DEMO_actions
{
//Analysis function The parameter is a reference to pluginManager
function __construct(&$pluginManager)
{
//Register this plug-in
//The first parameter is the name of the hook
//The second parameter is a reference to pluginManager
//The third is Methods executed by the plug-in
$pluginManager->register('demo', $this, 'say_hello');
}
function say_hello()
{
echo 'Hello World';
}
}
?>


For example, if I want to put say_hello on the homepage of my blog, Index.php, then you write somewhere in index.php: (author’s original words)

Copy the code The code is as follows:


$ pluginManager->trigger('demo','');


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

The above introduces how to use the sumproduct function and how to implement the website plug-in mechanism in PHP, including the use of the sumproduct function. I hope it will be helpful to friends who are interested in PHP tutorials.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template