ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
>[info]PHP 5 具有完整的反射 API,添加了对类、接口、函数、方法和扩展进行*反向工程*的能力。 此外,反射 API 提供了方法来取出函数、类和方法中的文档注释。 就算是类成员定义为private也可以在外部访问,不用创建类的实例也可以访问类的成员和方法 ## **反射类型** PHP反射API会基于类,方法,属性,参数等维护相应的反射类,已提供相应的调用API。 | 类型 | 说明 | | --- | --- | | Reflector | Reflector 是一个接口,被所有可导出的反射类所实现(implement) | | Reflection | 反射(reflection)类 | | ReflectionClass | 报告了一个类的有关信息 | | ReflectionZendExtension | 报告Zend扩展的相关信息 | | ReflectionExtension | 报告了PHP扩展的有关信息 | | ReflectionFunction | 报告了一个函数的有关信息 | | ReflectionFunctionAbstract | ReflectionFunction 的父类 | | ReflectionMethod | 报告了一个方法的有关信息 | | ReflectionObject | 报告了一个对象(object)的相关信息 | | ReflectionParameter | 取回了函数或方法参数的相关信息 | | ReflectionProperty | 报告了类的属性的相关信息 | ``` <?php class Bar{} $bar=new Bar(); class Foo { const LIKE="football"; public static $name = 'dash'; private static $id = 520; private $age=20; public function __construct(Bar $bar,$a,$b,$c) { //var_dump($bar); } public function say($content){ return (self::$name).'说了:'.$content; } } $reflect_class = new \ReflectionClass('Foo');//ReflectionClass::__set_state(array( 'name' => 'Foo', )) /** * 获取Foo类的实例 */ //创建类的新的实例。给出的参数将会传递到类的构造函数。接受可变数目的参数,用于传递到类的构造函数,和call_user_func()很相似 $foo_obj=$reflect_class->newInstance($bar,'__construct_param2','__construct_param3','...');//Foo::__set_state(array( 'age' => 20, )) //从给出的参数创建一个新的类实例,给出的参数将传递到类的构造函数 $foo_obj=$reflect_class->newInstanceArgs([$bar,'__construct_param2','__construct_param3','...']);//Foo::__set_state(array( 'age' => 20, )) //创建一个新的类实例而不调用它的构造函数 $foo_obj=$reflect_class->newInstanceWithoutConstructor();//Foo::__set_state(array( 'age' => 20, )) $foo_obj->say('haha');//"dash说了:haha" /** * 获取类常量的ReflectionClassConstant */ ////获取一组类常量的ReflectionClassConstant $reflection_constants=$reflect_class->getReflectionConstants();//array ( 0 => ReflectionClassConstant::__set_state(array( 'name' => 'LIKE', 'class' => 'Foo', )), ) //获取指定常量的ReflectionClassConstant $reflection_constant=$reflect_class->getReflectionConstant('LIKE');//ReflectionClassConstant::__set_state(array( 'name' => 'LIKE', 'class' => 'Foo', )) //获取由getReflectionConstant指定的LIKE常量的值 $cc=$reflection_constant->getValue();//'football' /** * 获取类属性的ReflectionProperty */ //获取类属性组成的数组 $reflected_propertys = $reflect_class->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);//array ( 0 => ReflectionProperty::__set_state(array( 'name' => 'name', 'class' => 'Foo', )), ) //获取Foo类指定属性 $reflected_property = $reflect_class->getProperty('age');//ReflectionProperty::__set_state(array( 'name' => 'age', 'class' => 'Foo', )) //设置静态属性的值 $reflect_class->setStaticPropertyValue('name', 'tom'); //在非公共的静态属性上调用此方法将返回一个ReflectionException,声明该属性不存在。 //$reflect_class->setStaticPropertyValue('id', 510);//会报the property does not exist的错 //用下面的方法绕过上面的报错问题 $reflected_property = $reflect_class->getProperty('id');//ReflectionProperty::__set_state(array( 'name' => 'id', 'class' => 'Foo', )) $reflected_property->setAccessible(true);//设置方法是否访问 //设置属性的值 $reflected_property->setValue(530); /** * 获取Foo类的方法ReflectionMethod */ //返回Foo类方法组成的信息数组 $reflect_methods=$reflect_class->getMethods();//array ( 0 => ReflectionMethod::__set_state(array( 'name' => '__construct', 'class' => 'Foo', )), 1 => ReflectionMethod::__set_state(array( 'name' => 'say', 'class' => 'Foo', )), ) //返回Foo类指定方法的信息 $reflect_method=$reflect_class->getMethod('__construct');//ReflectionMethod::__set_state(array( 'name' => '__construct', 'class' => 'Foo', )) //获取Foo类的构造函数的ReflectionMethod $reflect_method=$reflect_class->getConstructor(); //ReflectionMethod::__set_state(array( 'name' => '__construct', 'class' => 'Foo', )) /** * 获取Foo类某个方法的参数ReflectionParameter的数组 */ //返回由该方法参数组成的数组 $reflection_parameter=$reflect_method->getParameters();//array ( 0 => ReflectionParameter::__set_state(array( 'name' => 'bar', )), 1 => ReflectionParameter::__set_state(array( 'name' => 'a', )), 2 => ReflectionParameter::__set_state(array( 'name' => 'b', )), 3 => ReflectionParameter::__set_state(array( 'name' => 'c', )), ) /** * 执行__construct方法 */ $reflect_method->invoke($foo_obj,$bar,'param2','param3','...');//执行Foo类实例且由getMethod指定的方法 $reflect_method->invokeArgs ($foo_obj,[$bar,'param2','param3','...']);//带参数执行 /** * 执行say方法 */ $reflect_method=$reflect_class->getMethod('say');//ReflectionMethod::__set_state(array( 'name' => 'say', 'class' => 'Foo', )) echo $reflect_method->invoke($foo_obj,'哈哈');//tom说了:哈哈 echo $reflect_method->invokeArgs ($foo_obj,['嘻嘻']);//tom说了:嘻嘻 ```