使用反射动态加载第三方类
用反射加载第三方类用处在于:
使用xml或其他配文件配置要加载的类,从而和系统源代码分离。
对加载的类进行类检查,是加载的类符合自己定义的结构。
array(person => bob),ftpmodule => array(host => example.com, user => anon));private $modules = array();function init() { #初始化modulerunner,加载配置中的module$parent = new reflectionclass(module);foreach($this->configdata as $modulename => $params) { #检查配置中的module是否合法$moduleclass = new reflectionclass($modulename);if(! $moduleclass->issubclassof($parent)) { #检查是否是module的子类型throw new exception(unknown type : {$modulename});}$module = $moduleclass->newinstance();foreach($moduleclass->getmethods() as $method) { #检查配置中的函数的参数格式是否正确$this->handlemothod($module, $method, $params);}array_push($this->modules, $module); #加载module}}private function handlemothod(module $module, reflectionmethod $method, $params) { #检查module中的方法参数是
否和传入的$params名字相同,并且具有set方法
$name = $method->getname();$args = $method->getparameters();if(count($args) != 1 || substr($name, 0, 3) != set) { #如果没有配置中的类的方法的参数个数不为1,或者方法名前3个字母不为set,返回falsereturn false;}$property = strtolower(substr($name, 3));if(!isset($params[$property])) { #如果方法名后三个字母与配置中的参数名不同,返回falsereturn false;}$argclass = $args[0]->getclass(); #获取参数的类型if(empty($argclass)) {$method->invoke($module, $params[$property]); #参数无类型限制则直接调用set方法} else {$method->invoke($module, $argclass->newinstance($params[$property])); #有类型限制则新建一个实例并调用set方法}}public function getmodules() {return $this->modules;} }class person { #第三方类public $name;function __construct($name) {$this->name = $name;} }class ftpmodule extends module { #用户自定义第三方moduleprivate $host = default host;private $user = default user;function sethost($host) {$this->host = $host;}function setuser($user) {$this->user = $user;}function execute() {echo {$this->user} user {$this->host};} }class personmodule extends module { #用户自定义第三方moduleprivate $person;function setperson(person $person) {$this->person = $person;}function execute() {if(isset($person)) {echo i am {$this->person->name};} else {echo i am no user;}} }$modrunner = new modulerunner(); $modrunner->init(); var_dump($modrunner);?>
输出
object(modulerunner)#1 (2) { [configdata:modulerunner:private]=> array(2) { [personmodule]=> array(1) { [person]=> string(3) bob } [ftpmodule]=> array(2) { [host]=> string(11) example.com [user]=> string(4) anon } } [modules:modulerunner:private]=> array(2) { [0]=> object(personmodule)#4 (1) { [person:personmodule:private]=> object(person)#10 (1) { [name]=> string(3) bob } } [1]=> object(ftpmodule)#3 (2) { [host:ftpmodule:private]=> string(11) example.com [user:ftpmodule:private]=> string(4) anon } } }
通过反射获得类源码
getfilename(); #获取脚本文件文件名$file = file($path); #file()方法获取文件内容,并将内容保存在一个数组中,数组每个元素保存一行内容$start = $ref->getstartline(); #获取类在脚本中的第一行行号$end = $ref->getendline(); #获取类在脚本中最后一行的行号$source = implode(array_slice($file, $start - 1, $end - $start + 1)); #拼装类源码var_dump($source); }class person {public $age;private $name;function say() {echo yes;} }$ref = new reflectionclass(person); getsource($ref);?>