1、首先必须要有一个含有getName的接口
2、建立一个应用程序 去检测当前包含的插件(引用接口)
3、最后就是写你想要调用的插件程序。
<?php
/**
* 利用反射器插件
* @author wolf [Email: 116311316@qq.com]
*
*/
interface IPlugin
{
public function getName();
}
function findPlugins()
{
$plugins = array();
foreach (get_declared_classes() as $class) {
$rc = new ReflectionClass($class);
if ($rc->implementsInterface('IPlugin')) {
$plugins[] = $rc;
}
}
return $plugins;
}
function ComputeMenu()
{
$menu = array();
foreach (findPlugins() as $plugin) {
if ($plugin->hasMethod('getUserName')) {
$reflectionMethod = $plugin->getMethod('getUserName');
if ($reflectionMethod->isStatic()) {
$items = $reflectionMethod->invoke(null);
} else {
$pluginInstance = $plugin->newInstance();
$items = $reflectionMethod->invoke($pluginInstance);
}
$menu = array_merge($menu, $items);
}
}
return $menu;
}
/**
* this is your plugin
* @author wolf
*
*/
class MyCoolPlugin implements IPlugin
{
public function getName()
{
return 'MyCoolPlugin';
}
public static function getMenuItems()
{
return array(
array(
'decription' => 'MyCoolPlugin',
'link' => '/MyCoolPlugin'));
}
}
/**
* this is another plugin
* @author wolf
*
*/
class OtherPlugin implements IPlugin
{
public function getName(){
return 'OtherPlugin';
}
public function getUserName(){
return array('name'=>'wolf','sex'=>'man');
}
}
$menu = ComputeMenu();//call application
print_r($menu);