ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
>报告了一个类的有关信息 ``` ReflectionClass implements Reflector { /* 常量 */ const integer IS_IMPLICIT_ABSTRACT = 16;//指示了类是一个抽象类(abstract), 因为它有抽象(abstract)方法 const integer IS_EXPLICIT_ABSTRACT = 32 ;//指示了类是一个抽象类(abstract), 因为它已明确定义。 const integer IS_FINAL = 64 ;//指示这是一个 final 类.//类的名称。只读,并在尝试赋值的时候会抛出 ReflectionException /* 属性 */ public $name ; /* 方法 */ public __construct ( mixed $argument ) //初始化 ReflectionClass 类 public static export ( mixed $argument [, bool $return = false ] ) : string //导出一个类 public getConstant ( string $name ) : mixed //获取定义过的一个常量 public getConstants ( void ) : array//获取一组常量 public getConstructor ( void ) : ReflectionMethod//获取类的构造函数 public getDefaultProperties ( void ) : array //获取默认属性 public getDocComment ( void ) : string //获取文档注释 public getEndLine ( void ) : int //获取最后一行的行数 public getExtension ( void ) : ReflectionExtension //根据已定义的类获取所在扩展的 ReflectionExtension 对象 public getExtensionName ( void ) : string //获取定义的类所在的扩展的名称 public getFileName ( void ) : string //获取定义类的文件名 public getInterfaceNames ( void ) : array //获取接口(interface)名称 public getInterfaces ( void ) : array //获取接口 public getMethod ( string $name ) : ReflectionMethod //获取一个类方法的 ReflectionMethod public getMethods ([ int $filter ] ) : array //获取所有方法组成的数组 public getModifiers ( void ) : int //获取类的修饰符 public getName ( void ) : string //获取类名 public getNamespaceName ( void ) : string //获取命名空间的名称 public getParentClass ( void ) : ReflectionClass //获取父类 public getProperties ([ int $filter ] ) : array //获取一组属性 public getProperty ( string $name ) : ReflectionProperty //获取类的一个属性的 ReflectionProperty public getReflectionConstant ( string $name ) : ReflectionClassConstant //获取类的常量的ReflectionClassConstant public getReflectionConstants ( void ) : array //获取类常量 public getShortName ( void ) : string //获取短名 public getStartLine ( void ) : int //获取起始行号 public getStaticProperties ( void ) : array //获取静态(static)属性 public getStaticPropertyValue ( string $name [, mixed &$def_value ] ) : mixed //获取静态(static)属性的值 public getTraitAliases ( void ) : array //返回 trait 别名的一个数组 public getTraitNames ( void ) : array //返回这个类所使用 traits 的名称的数组 public getTraits ( void ) : array //返回这个类所使用的 traits 数组 public hasConstant ( string $name ) : bool //检查常量是否已经定义 public hasMethod ( string $name ) : bool //检查方法是否已定义 public hasProperty ( string $name ) : bool //检查属性是否已定义 public implementsInterface ( string $interface ) : bool //接口的实现 public inNamespace ( void ) : bool //检查是否位于命名空间中 public isAbstract ( void ) : bool //检查类是否是抽象类(abstract) public isAnonymous ( void ) : bool //检查类是否是匿名类 public isCloneable ( void ) : bool //返回了一个类是否可复制 public isFinal ( void ) : bool //检查类是否声明为 final public isInstance ( object $object ) : bool //检查类的实例 public isInstantiable ( void ) : bool //检查类是否可实例化 public isInterface ( void ) : bool //检查类是否是一个接口(interface) public isInternal ( void ) : bool //检查类是否由扩展或核心在内部定义 public isIterable ( void ) : bool //检查此类是否可迭代 public isIterateable ( void ) : bool //检查是否可迭代(iterateable) public isSubclassOf ( string $class ) : bool //检查是否为一个子类 public isTrait ( void ) : bool //返回了是否为一个 trait public isUserDefined ( void ) : bool //检查是否由用户定义的 public newInstance ( mixed $args [, mixed $... ] ) : object //创建类的新的实例。给出的参数将会传递到类的构造函数。接受可变数目的参数,用于传递到类的构造函数,和call_user_func()很相似 public newInstanceArgs ([ array $args ] ) : object //从给出的参数创建一个新的类实例,给出的参数将传递到类的构造函数 public newInstanceWithoutConstructor ( void ) : object //创建一个新的类实例而不调用它的构造函数 public setStaticPropertyValue ( string $name , string $value ) : void //设置静态属性的值 public __toString ( void ) : strin //返回 ReflectionClass 对象字符串的表示形式 } ``` # **例子:** ## **检查类** ``` function classData(ReflectionClass $class) { $details = ''; $name = $class->getName(); // 返回要检查的类名 if ($class->isUserDefined()) { // 检查类是否由用户定义 $details .= "$name 类是用户自定义的" . PHP_EOL; } if ($class->isInternal()) { // 检查类是否由扩展或核心在内部定义 $details .= "$name is built-in" . PHP_EOL; } if ($class->isInterface()) { // 检查类是否是一个接口 $details .= "$name 是一个接口类" . PHP_EOL; } if ($class->isAbstract()) { // 检查类是否是抽象类 $details .= "$name 是一个抽象类" . PHP_EOL; } if ($class->isFinal()) { // 检查类是否声明为 final $details .= "$name 是一个final类" . PHP_EOL; } if ($class->isInstantiable()) { // 检查类是否可实例化 $details .= "$name 类可以被实例化" . PHP_EOL; } else { $details .= "$name 类不能实例化" . PHP_EOL; } return $details; } $prodClass = new ReflectionClass('User'); print classData($prodClass); ``` ## **获取内容信息** ``` function getClassSource(ReflectionClass $class) { $path = $class->getFileName(); // 获取类文件的绝对路径 $lines = @file($path); // 由路径获得由文件中所有行组成的数组(包括User以外的整个文件内容) $from = $class->getStartLine(); // 提供类的起始行 $to = $class->getEndLine(); // 提供类的终止行 $len = $to - $from + 1; return implode(array_slice($lines, $from - 1, $len));//提取User类部分并将此数组拼接为字符串 } $prodClass = new ReflectionClass('User'); var_dump(getClassSource($prodClass)); ``` ## **# 检查方法** >[info]* 获得 ReflectionFunctionAbstract 下的ReflectionMethod 对象的方法有两种:第一种是通过 ReflectionClass::getMethods() 获得 ReflectionMethod 对象的数组,会返回类中所有方法的 ReflectionMethod  对象。无需知道方法名 >* 第二种是直接使用 ReflectionMethod  类实例化对象:`new ReflectionMethod('User', 'getName');`,这种方式只能获取一个类方法对象,需要提前知道方法名。 ReflectionMethod 对象的方法例子参考下节的ReflectionMethod类 ``` //调用B的show方法时候去调用A的show方法 class A{ function show($param=20){ echo "classA的show方法,参数值为{$param}"; } } class B{ private $obj; function __construct(){ $this->obj = new A(); } function __call($name, $arguments) { $ref = new ReflectionClass($this->obj); //检查方法是否已定义 if ($ref->hasMethod($name)){ //获取一个类方法的 ReflectionMethod对象。相当于new ReflectionMethod('class', 'myMethod') $method = $ref->getMethod($name); if ($method->isPublic()&&!$method->isAbstract()&&count($arguments)==0){ if ($method->isStatic()){ //如果是静态方法,不需要传入调用的对象 $method->invoke(null); }else{ $method->invoke($this->obj); } }else{ if ($method->isStatic()){ //如果是静态方法,不需要传入调用的对象 $method->invokeArgs(null,$arguments); }else{ $method->invokeArgs($this->obj,$arguments); } } } } } $b=new B(); $b->show();//classA的show方法,参数值为20 $b->show(21);//classA的show方法,参数值为21 ``` 插件案例 ~~~ include_once __DIR__."/plugin.php"; function get_plugin_menus(){ $menus = array(); $all_class = get_declared_classes();//获取所有的类 foreach ($all_class as $cls){ $ref_cls = new ReflectionClass($cls); if ($ref_cls->implementsInterface('Plugin')){//是否实现了某个接口 if ($ref_cls->hasMethod('showMenu')){ $method = $ref_cls->getMethod("showMenu"); if ($method->isStatic()){ $method->invoke(null); } else{ //$method->invoke(new $cls());//这样获取类 $instance = $ref_cls->newInstance(); $menu = $method->invoke($instance); } } } $menus = array_merge($menus,$menu); } return $menus; } echo "<pre>";get_plugin_menus();echo "<pre>"; interface Plugin{ function showMenu(); } class MyPlugin implements Plugin{ function showMenu() { $menu = array( array( 'name' => 'menu1', 'link' => 'index.php?act=link1' ), array( 'name' => 'menu2', 'link' => 'index.php?act=link2' ) ); return $menu; } ~~~