getParameters();foreach ($params as $param) { echo "param name:".$param->getName().""; if($param->isOptional()) { echo "Default value:".$param->getDefaultValue().""; } if($param->allowsNull() ===true) { echo "可以为空"; }else{ echo "不能为空"; } echo "=================";}*///Reflection::export(new ReflectionExtension('reflection'));//打印出reflection api实例的每一个类,方法和参数细节//我们通过Reflection的export()方法实现这种输出//get_declared_classes() 返回每一个内建的php类包括系统类库/*foreach (get_declared_classes() as $class) { $reflectionClass = Reflection::export(new ReflectionClass($class)); //echo "============================"; if($reflectionClass->isUserDefined()===true) //用户自定义的类 { Reflection::export($reflectionClass); echo "============================"; }}*//*class ClassOne{ public function callClassOne($arr) { print "In Class One, str:{$arr[0]}"; }}class ClassOneDelegator{ private $targets; function __construct() { $this->targets[] = new ClassOne(); } //__call()函数在一个对象的上下文中,如果调用的方法不存在的时候,它被触发 function __call($name, $args) { //echo $name.""; //var_dump($args); foreach($this->targets as $obj) { $r = new ReflectionClass($obj); //取得该类的详细信息 //var_dump($r->getMethod($name)); if($method =$r->getMethod($name)) //获取该类是否有$name这个方法 { //方法为公有,并且测试该类是否为抽象类 if($method->isPublic() && !$method->isAbstract()) { return $method->invoke($obj,$args); //invoke 调用该方法 } } } }}$obj = new ClassOneDelegator();$str = 'I love You';$obj->callClassOne($str);*///PHP反射 API--利用反射 技术实现的插件系统架构interface Iplugin{ public static function getName();}//查询插件函数function findPlugins(){ $plugins = array(); foreach (get_declared_classes() as $class) { //获取所有已经加载的类 $reflectionClass = new ReflectionClass($class); //implementsInterface() 测试是否实现了某个特定的接口 if($reflectionClass->implementsInterface('Iplugin')) { $plugins[] = $reflectionClass; } } return $plugins;}//计算菜单function computeMenu(){ $menu = array(); foreach(findPlugins() as $plugin) { //hasMethod 测试判断是否有特定的方法 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 computeArtides(){ $artides = array(); foreach(findPlugins() as $plugin) { if($plugin->hasMethod('getArtides')) { $reflectionMethod = $plugin->getMethod('getArtides'); if($reflectionMethod->isStatic()) { $items = $reflectionMethod->invoke(null); }else{ $pluginInstance = $plugin->newInstance(); $items = $reflectionMethod->invoke($pluginInstance); } $artides = array_merge($artides, $items); } } return $artides;}class MyCoolPugin implements Iplugin{ public static function getName() { return "MycoolPlugin"; } public static function getMenuItems() { return array(array('description'=>'MycoolPlugin','link'=>'/MyCoolPlugin')); } public function getArtides() { return array(array('path'=>'/MycoolPlugin','title'=>'this is a really cool artide','text'=>'xianglingchuan')); }}$menu = computeMenu();$artides = computeArtides();var_dump($menu);echo "=================";var_dump($artides);echo "=================";?>