利用反射api来加载模块
2011-05-16 21:55
357 查看
<?php /* * Created on 2011-5-16 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ //假设我们要创建要给类来动态调用Module对象,即该类可以自由加载第三 //方插件并集成进已有的系统,而不需要把第三方的代码硬编码进原有的代 //码。要达到这个目的,可以在Module接口或抽象类中定义一个execute() //方法,强制要求所有的子类都必须实现该方法。可以允许用户在外部XML配 //置文件中列出所有Module类。系统可以使用XML提供的信息来加载一定数目 //的Module对象,然后调用其中的execute(). // // 然而,如果每个Module需要不同的信息来完成任务,应该怎么做呢? // 这这种情况下,XML文件可以为每个Module提供属性键和值,Module // 的创建者可以为每个属性名提供setter方法。代码要确保根据某个属 // 性名调用正确的setter方法。 class Person { public $name; function __construct($name) { $this->name = $name; } } interface Module{ function execute(); } class FtpModule implements Module{ function setHost($host){ print "FtpModule::setHost():$host\n<br>"; } function setUser($user) { print "FtpModule::setUser():$user\n<br>"; } function execute() { } } class PersonModule implements Module{ function setPerson(Person $person) { print "PersonModule::setPerson():{$person->name}\n<br>"; } function execute() { } } class ModuleRunner { private $modules = array(); private $configData = array( "PersonModule"=> array('person'=>'bob'), "FtpModule"=> array('host'=>'example.com','user'=>'anon' )); public function init() { $interface = new ReflectionClass('Module'); foreach($this->configData as $modulename =>$params) { $module_class = new ReflectionClass($modulename); if(!$module_class->isSubclassOf($interface)){ throw new Exception("unkown module type:$modulename"); } $module = $module_class->newInstance(); foreach($module_class->getMethods() as $method ) { $this->handleMethod($module,$method,$params); } array_push($this->modules,$module); } } public function handleMethod(Module $module,ReflectionMethod $method,$params) { $name = $method->getName(); $args = $method->getParameters(); if(count($args)!=1 || substr($name,0,3)!="set") { return false; } $property= strtolower(substr($name,3)); if(!isset($params[$property]) ){ return false;} $arg_class = $args[0]->getClass(); if(emptyempty($arg_class) ){ $method->invoke($module,$params[$property]); }else{ $method->invoke($module,$arg_class->newInstance($params[$property])); } } } $test = new ModuleRunner(); $test->init(); ?>
相关文章推荐
- C#如何通过反射实现动态功能模块加载
- C# aspx页面动态加载ascx用户控件 及 利用反射调用其
- 27 API-反射(类的加载器,反射的使用,动态代理)&设计模式(装饰设计模式,模版设计模式)&JDK新特性(JDK5,JDK6,JDK7,DK8)
- C#利用反射来加载程序集,并调用程序集中有关类的方法
- 利用HTML5+的Uploader模块API实现基于MUI的文件上传
- android学习日记:利用反射调用隐藏API
- 利用Dojo和JSON建立无限级AJAX动态加载的功能模块树
- 如何通过反射实现动态功能模块加载
- 利用反射进行动态加载dll
- c# 利用反射给控件加载事件(以button为例)
- 109_利用反射调用隐藏api获取应用程序的缓存大小
- 利用反射实现窗体动态加载
- PHP反射API实现自动加载
- 利用Sap2000API和C#开发插件的小例子(添加混凝土材料模块)
- Android:利用Java反射调用@hide的API
- Android:利用Java反射调用@hide的API
- C#利用反射动态加载DLL(C#)并调用其中的接口
- [转] 通过反射实现动态功能模块加载
- 自定义类加载器,利用反射加载任意路径下的class文件
- 利用反射api查找一个类的详细信息