php實(shí)現(xiàn)網(wǎng)站插件機(jī)制的方法
Jun 13, 2016 pm 12:20 PM
首先是插件的管理類的實(shí)現(xiàn):
復(fù)制代碼 代碼如下:
/**
* STBLOG PluginManager Class
*
* 插件機(jī)制的實(shí)現(xiàn)核心類
*
* @package STBLOG
* @subpackage Libraries
* @category Libraries
* @author Saturn
* @link http://www.cnsaturn.com/
*/
class PluginManager
{
/**
* 監(jiān)聽已注冊的插件
*
* @access private
* @var array
*/
private $_listeners = array();
/**
* 構(gòu)造函數(shù)
*
* @access public
* @return void
*/
public function __construct()
{
#這里$plugin數(shù)組包含我們獲取已經(jīng)由用戶激活的插件信息
#為演示方便,我們假定$plugin中至少包含
#$plugin = array(
# 'name' => '插件名稱',
# 'directory'=>'插件安裝目錄'
#);
$plugins = get_active_plugins();#這個(gè)函數(shù)請自行實(shí)現(xiàn)
if($plugins)
{
foreach($plugins as $plugin)
{//假定每個(gè)插件文件夾中包含一個(gè)actions.php文件,它是插件的具體實(shí)現(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)
{
//獲取插件要實(shí)現(xiàn)的方法
$key = get_class($reference).'->'.$method;
//將插件的引用連同方法push進(jìn)監(jiān)聽數(shù)組中
$this->_listeners[$hook][$key] = array(&$reference, $method);
#此處做些日志記錄方面的東西
}
/**
* 觸發(fā)一個(gè)鉤子
*
* @param string $hook 鉤子的名稱
* @param mixed $data 鉤子的入?yún)?
* @return mixed
*/
function trigger($hook, $data='')
{
$result = '';
//查看要實(shí)現(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))
{
// 動(dòng)態(tài)調(diào)用插件的方法
$result .= $class->$method($data);
}
}
}
#此處做些日志記錄方面的東西
return $result;
}
}
?>
然后是插件的具體實(shí)現(xiàn)方法:
復(fù)制代碼 代碼如下:
/**
* 這是一個(gè)Hello World簡單插件的實(shí)現(xiàn)
*
* @package DEMO
* @subpackage DEMO
* @category Plugins
* @author Saturn
* @link http://www.cnsaturn.com/
*/
/**
*需要注意的幾個(gè)默認(rèn)規(guī)則:
* 1. 本插件類的文件名必須是action
* 2. 插件類的名稱必須是{插件名_actions}
*/
class DEMO_actions
{
//解析函數(shù)的參數(shù)是pluginManager的引用
function __construct(&$pluginManager)
{
//注冊這個(gè)插件
//第一個(gè)參數(shù)是鉤子的名稱
//第二個(gè)參數(shù)是pluginManager的引用
//第三個(gè)是插件所執(zhí)行的方法
$pluginManager->register('demo', $this, 'say_hello');
}
function say_hello()
{
echo 'Hello World';
}
}
?>
比如我要將say_hello放到我博客首頁Index.php,那么你在index.php中的某個(gè)位置寫下:(作者原話)
復(fù)制代碼 代碼如下:
$pluginManager->trigger('demo','');
以上就是一個(gè)插件機(jī)制的實(shí)現(xiàn),over!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

Yes,aPythonclasscanhavemultipleconstructorsthroughalternativetechniques.1.Usedefaultargumentsinthe__init__methodtoallowflexibleinitializationwithvaryingnumbersofparameters.2.Defineclassmethodsasalternativeconstructorsforclearerandscalableobjectcreati

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.
