php利用反射实现插件机制的方法
程序员文章站
2022-05-25 14:29:13
本文实例讲述了php利用反射实现插件机制的方法。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:
本文实例讲述了php利用反射实现插件机制的方法。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:
<?php
/**
* @name php反射api--利用反射技术实现的插件系统架构
*/
interface iplugin{
public static function getname();
}
function findplugins(){
$plugins = array();
foreach (get_declared_classes() as $class){
$reflectionclass = new reflectionclass($class);
if ($reflectionclass->implementsinterface('iplugin')) {
$plugins[] = $reflectionclass;
}
}
return $plugins;
}
function computemenu(){
$menu = array();
foreach (findplugins() as $plugin){
if ($plugin->hasmethod('getmenuitems')) {
$reflectionmethod = $plugin->getmethod('getmenuitems');
if ($reflectionmethod->isstatic()) {
$items = $reflectionmethod->invoke(null);
} else {
$plugininstance = $plugin->newinstance();
$items = $reflectionmethod->invoke($plugininstance);
}
$menu = array_merge($menu,$items);
}
}
return $menu;
}
function computearticles(){
$articles = array();
foreach (findplugins() as $plugin){
if ($plugin->hasmethod('getarticles')) {
$reflectionmethod = $plugin->getmethod('getarticles');
if ($reflectionmethod->isstatic()) {
$items = $reflectionmethod->invoke(null);
} else {
$plugininstance = $plugin->newinstance();
$items = $reflectionmethod->invoke($plugininstance);
}
$articles = array_merge($articles,$items);
}
}
return $articles;
}
class mycoolpugin implements iplugin {
public static function getname(){
return 'mycoolplugin';
}
public static function getmenuitems(){
return array(array('description'=>'mycoolplugin','link'=>'/mycoolplugin'));
}
public static function getarticles(){
return array(array('path'=>'/mycoolplugin','title'=>'this is a really cool article','text'=> 'xxxxxxxxx' ));
}
}
$menu = computemenu();
$articles = computearticles();
print_r($menu);
print_r($articles);
/**
* @name php反射api--利用反射技术实现的插件系统架构
*/
interface iplugin{
public static function getname();
}
function findplugins(){
$plugins = array();
foreach (get_declared_classes() as $class){
$reflectionclass = new reflectionclass($class);
if ($reflectionclass->implementsinterface('iplugin')) {
$plugins[] = $reflectionclass;
}
}
return $plugins;
}
function computemenu(){
$menu = array();
foreach (findplugins() as $plugin){
if ($plugin->hasmethod('getmenuitems')) {
$reflectionmethod = $plugin->getmethod('getmenuitems');
if ($reflectionmethod->isstatic()) {
$items = $reflectionmethod->invoke(null);
} else {
$plugininstance = $plugin->newinstance();
$items = $reflectionmethod->invoke($plugininstance);
}
$menu = array_merge($menu,$items);
}
}
return $menu;
}
function computearticles(){
$articles = array();
foreach (findplugins() as $plugin){
if ($plugin->hasmethod('getarticles')) {
$reflectionmethod = $plugin->getmethod('getarticles');
if ($reflectionmethod->isstatic()) {
$items = $reflectionmethod->invoke(null);
} else {
$plugininstance = $plugin->newinstance();
$items = $reflectionmethod->invoke($plugininstance);
}
$articles = array_merge($articles,$items);
}
}
return $articles;
}
class mycoolpugin implements iplugin {
public static function getname(){
return 'mycoolplugin';
}
public static function getmenuitems(){
return array(array('description'=>'mycoolplugin','link'=>'/mycoolplugin'));
}
public static function getarticles(){
return array(array('path'=>'/mycoolplugin','title'=>'this is a really cool article','text'=> 'xxxxxxxxx' ));
}
}
$menu = computemenu();
$articles = computearticles();
print_r($menu);
print_r($articles);
希望本文所述对大家的php程序设计有所帮助。
上一篇: php实现字符串反转输出的方法
下一篇: php写入数据到CSV文件的方法