php利用反射实现插件机制的方法
程序员文章站
2023-04-06 19:27:55
本文实例讲述了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程序设计有所帮助。