国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home 類庫下載 PHP類庫 How to implement website plug-in mechanism in php

How to implement website plug-in mechanism in php

Oct 14, 2016 am 10:18 AM

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

* @subpackage Libraries
* @category Libraries
* @author Saturn
* @link http://www.cnsaturn.com/
*/
class PluginManager
{
/**
* 監(jiān)聽已注冊的插件
*
* @access private
* @var array
*/
private $_listeners = array();
/**
* 構造函數(shù)
*
* @access public
* @return void
*/
public function __construct()
{
#這里$plugin數(shù)組包含我們獲取已經(jīng)由用戶激活的插件信息
#為演示方便,我們假定$plugin中至少包含
#$plugin = array(
# 'name' => '插件名稱',
# 'directory'=>'插件安裝目錄'
#);
$plugins = get_active_plugins();#這個函數(shù)請自行實現(xiàn)
if($plugins)
{
foreach($plugins as $plugin)
{//假定每個插件文件夾中包含一個actions.php文件,它是插件的具體實現(xiàn)
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))
{
//初始化所有插件
new $class($this);
}
}
}
}
#此處做些日志記錄方面的東西
}
/**
* 注冊需要監(jiān)聽的插件方法(鉤子)
*
* @param string $hook
* @param object $reference
* @param string $method
*/
function register($hook, &$reference, $method)
{
//獲取插件要實現(xiàn)的方法
$key = get_class($reference).'->'.$method;
//將插件的引用連同方法push進監(jiān)聽數(shù)組中
$this->_listeners[$hook][$key] = array(&$reference, $method);
#此處做些日志記錄方面的東西
}
/**
* 觸發(fā)一個鉤子
*
* @param string $hook 鉤子的名稱
* @param mixed $data 鉤子的入?yún)?
* @return mixed
*/
function trigger($hook, $data='')
{
$result = '';
//查看要實現(xiàn)的鉤子,是否在監(jiān)聽數(shù)組之中
if (isset($this->_listeners[$hook]) && is_array($this->_listeners[$hook]) && count($this->_listeners[$hook]) > 0)
{
// 循環(huán)調(diào)用開始
foreach ($this->_listeners[$hook] as $listener)
{
// 取出插件對象的引用和方法
$class =& $listener[0];
$method = $listener[1];
if(method_exists($class,$method))
{
// 動態(tài)調(diào)用插件的方法
$result .= $class->$method($data);
}
}
}
#此處做些日志記錄方面的東西
return $result;
}
}
?>
 
然后是插件的具體實現(xiàn)方法
 
 
<?
/**
* 這是一個Hello World簡單插件的實現(xiàn)
*
* @package DEMO
* @subpackage DEMO
* @category Plugins
* @author Saturn
* @link http://www.cnsaturn.com/
*/
/**
*需要注意的幾個默認規(guī)則:
* 1. 本插件類的文件名必須是action
* 2. 插件類的名稱必須是{插件名_actions}
*/
class DEMO_actions
{
//解析函數(shù)的參數(shù)是pluginManager的引用
function __construct(&$pluginManager)
{
//注冊這個插件
//第一個參數(shù)是鉤子的名稱
//第二個參數(shù)是pluginManager的引用
//第三個是插件所執(zhí)行的方法
$pluginManager->register(&#39;demo&#39;, $this, &#39;say_hello&#39;);
}
function say_hello()
{
echo &#39;Hello World&#39;;
}
}
?>

For example, if I want to put say_hello on the homepage of my blog, Index.php, then you write somewhere in index.php: The code is as follows:
$pluginManager->trigger ('demo','');

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276