助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
``` ReflectionMethod extends ReflectionFunctionAbstract implements Reflector { /* 常量 */ const integer IS_STATIC = 1 ; //指示一个方法是静态(static)的。 const integer IS_PUBLIC = 256 ; //指示一个方法是 public 的。 const integer IS_PROTECTED = 512 ; //指示一个方法是 protected 的。 const integer IS_PRIVATE = 1024 ; //指示一个方法是 private 的。 const integer IS_ABSTRACT = 2 ; //指示一个方法是 abstract 的 const integer IS_FINAL = 4 ; //指示一个方法是 final 的 /* 属性 */ public $name ; //方法名 public $class ; //类名 /* 方法 */ public __construct ( mixed $class , string $name ) //ReflectionMethod 的构造函数 public static export ( string $class , string $name [, bool $return = false ] ) : string //输出一个回调方法*** public getClosure ( object $object ) : Closure //返回一个动态建立的方法调用接口,译者注:可以使用这个返回值直接调用非公开方法。 public getDeclaringClass ( void ) : ReflectionClass //获取反射函数调用参数的类表达 public getModifiers ( void ) : int //获取方法的修饰符 public getPrototype ( void ) : ReflectionMethod //返回方法原型 (如果存在) public invoke ( object $object [, mixed $parameter [, mixed $... ]] ) : mixed // 执行? public invokeArgs ( object $object , array $args ) : mixed //带参数执行 public isAbstract ( void ) : bool //判断方法是否是抽象方法 public isConstructor ( void ) : bool //判断方法是否是构造方法 public isDestructor ( void ) : bool //判断方法是否是析构方法 public isFinal ( void ) : bool //判断方法是否定义 final public isPrivate ( void ) : bool // 判断方法是否是私有方法 public isProtected ( void ) : bool //判断方法是否是保护方法 (protected) public isPublic ( void ) : bool //判断方法是否是公开方法 public isStatic ( void ) : bool //判断方法是否是静态方法 public setAccessible ( bool $accessible ) : void //设置方法是否访问 public __toString ( void ) : string //返回反射方法对象的字符串表达 /* 继承ReflectionFunctionAbstract的方法 */ final private __clone ( void ) : void //复制函数 public getClosureScopeClass ( void ) : ReflectionClass //返回与闭包关联的作用域 public getClosureThis ( void ) : object //返回本身的匿名函数 public getDocComment ( void ) : string //获取注释内容*** public getEndLine ( void ) : int //获取结束行号*** public getExtension ( void ) : ReflectionExtension //获取扩展信息*** public getExtensionName ( void ) : string //获取扩展名称*** public getFileName ( void ) : string //获取文件名称*** public getName ( void ) : string // 获取函数名称*** public getNamespaceName ( void ) : string //获取命名空间*** public getNumberOfParameters ( void ) : int //获取参数数目 public getNumberOfRequiredParameters ( void ) : int //获取必须输入参数个数 public getParameters ( void ) : array //获取参数 public getReturnType ( void ) : ReflectionType //获取函数的指定返回类 public getShortName ( void ) : string //获取函数短名称*** public getStartLine ( void ) : int //获取开始行号*** public getStaticVariables ( void ) : array //获取静态变量 public hasReturnType ( void ) : bool //检查函数是否具有指定的返回类型 public inNamespace ( void ) : bool //检查是否处于命名空间*** public isClosure ( void ) : bool //检查是否是匿名函数 public isDeprecated ( void ) : bool // 检查是否已经弃用 public isGenerator ( void ) : bool //判断函数是否是一个生成器函数 public isInternal ( void ) : bool //判断函数是否是内置函数*** public isUserDefined ( void ) : bool // 检查是否是用户定义*** public isVariadic ( void ) : bool //检查函数是否为可变参数 public returnsReference ( void ) : bool //检查是否返回参考信息 abstract public __toString ( void ) : void //字符串化当对象被当做字符串时触发 } ``` # **例子:** ``` class User{ private $name='张三'; private $age='18'; function getname(){ echo $this->name; } protected function getage(){ echo $this->$age; } protected function setInfo($name,$age){ $this->age=$age; $this->name=$name; } } //建立反射 $class=new reflectionClass('User'); $methdo=$class->getMethods(); //获取所有方法 组成的对象 var_export($methdo); /* array ( 0 => ReflectionMethod::__set_state(array( 'name' => 'getname', 'class' => 'User', )), 1 => ReflectionMethod::__set_state(array( 'name' => 'getage', 'class' => 'User', )), 2 => ReflectionMethod::__set_state(array( 'name' => 'setInfo', 'class' => 'User', )), ) */ //无需new User()实例化获得类实例 $instentce=$class->newInstance(); var_export($instentce); /* User::__set_state(array( 'name' => '张三', 'age' => '18', )) */ //获得指定方的法对象 $method = new ReflectionMethod('User', 'setInfo'); var_export($method); /* ReflectionMethod::__set_state(array( 'name' => 'setInfo', 'class' => 'User', )) */ //获得指定方法的参数 $params = $method->getParameters(); var_export($params); /* array ( 0 => ReflectionParameter::__set_state(array( 'name' => 'name', )), 1 => ReflectionParameter::__set_state(array( 'name' => 'age', )), ) */ ```