当前位置: 首页 > 后端技术 > PHP

Reflection

时间:2023-03-30 03:50:20 PHP

什么是ReflectionReflection是面向对象范式中用于操作元模型的API,可用于构建复杂的、可扩展的应用程序。反射的作用反射的主要目的是分析类或对象在运行时的状态,派生或提取类、方法、属性、参数等详细信息,包括注解。反射的应用反射在日常的web开发中用处不大,更多的是用在偏底层的代码中,比如依赖注入、对象池、动态代理、自动获取插件列表、自动生成框架的底层。文档和一些设计模式等,都会大量使用反射技术。常用的反射类在日常开发中用的不多,所以暂时只学习一些比较常用的反射类,其他的内容以后再深入学习。比较常用的反射类:ReflectionClass:报告某个类的信息;ReflectionFunction:报告有关方法的信息;ReflectionMethod:报告有关函数的信息;ReflectionParameter:检索有关函数或方法参数的信息。示例name=$name;$this->year=$year;}publicfunctionsetBase(Printer$printer,$name,$year){$this->name=$name;$this->year=$year;}publicfunctiongetValue(){return$this->name;}}classPrinter{}测试ReflectionClass类:$reflClass=newReflectionClass('Student');echoget_calss($reflClass);$s=$reflClass->newInstanceArgs(['程心',18]);echo$s->getValue();输出如下:[Running]php"d:\phpstudy_pro\WWW\Reflection\Foo.php"ReflectionClassChengXin[Done]exitedwithcode=0in1.198seconds这里需要注意的是:newInstanceArgs方法模拟手动实例化对象,但是方法的参数是一个数组。测试ReflectionClass类和ReflectionParameter类:$reflClass=newReflectionClass('Student');$reflMethod=$reflClass->getMethod('setBase');echoget_class($reflMethod)。PHP_EOL;$params=$reflMethod->getParameters();var_dump($params);foreach($paramsas$param){echo$param->getName().PHP_EOL;如果($param->getType()!=NULL){echo$param->getType()->getName()。PHP_EOL;}if($param->isDefaultValueAvailable()){echo$param->getDefaultValue()。PHP_EOL;}回声'--------'。PHP_EOL;}输入如下:[Running]php"d:\phpstudy_pro\WWW\Reflection\Foo.php"ReflectionMethodarray(3){[0]=>object(ReflectionParameter)#3(1){["name"]=>string(7)"打印机"}[1]=>对象(反射参数)#4(1){["名称"]=>字符串(4)"名称"}[2]=>对象(反射参数)#5(1){["name"]=>string(4)"year"}}printerPrinter--------name--------year10--------[Done]exitedwithcode=0in0.274seconds我们调用反射类的getMethod方法,参数为Student类中setBase方法的方法名setBase,该方法返回一个ReflectionMethod类,如控制台第一行输出。ReflectionMethod类有一个方法叫getParameters,返回一个ReflectionMethod对应的方法的所有参数的数组,这个数组的每个元素都是ReflectionParameter类的一个对象。下面的foreach遍历所有参数,首先检查$parameter->getType()的返回值。如果指定参数类型,ReflectionParameter类的getType方法返回ReflectionType对象,否则返回null:如果指定参数类型,则返回ReflectionType对象,否则返回null。ReflectionType类上报函数的参数/返回类型和类信息的属性类型,并提供getName方法获取参数类型的名称。ReflectionParameter类还有一个比较常用的方法——isDefaultValueAvailable,用来检查这个参数是否有默认值。如果该参数有默认值,可以使用getDefaultValue方法获取默认值。用于测试ReflectionFunction类:functiondisplay($a,$b,Printer$printer){echo"called"."\n";}$reflFunction=newReflectionFunction("display");echoget_class($reflFunction)。PHP_EOL;$params=$reflFunction->getParameters();var_dump($params);foreach($paramsas$param){echo$param->getName().PHP_EOL;如果($param->getType()!=null){echo$param->getType()->getName()。PHP_EOL;}if($param->isDefaultValueAvailable()){echo$param->getDefaultValue()。PHP_EOL;}回声'--------'。PHP_EOL;}输出如下:[Running]php"d:\phpstudy_pro\WWW\Reflection\Foo.php"ReflectionFunctionarray(3){[0]=>object(ReflectionParameter)#2(1){["name"]=>string(1)"a"}[1]=>object(ReflectionParameter)#3(1){["name"]=>string(1)"b"}[2]=>object(ReflectionParameter)#4(1){["名称"]=>;string(7)"printer"}}a--------b--------printerPrinter--------[Done]在0.247秒内以code=0退出,并且ReflectionMethod就像反射类,ReflectionFunction提供getParameters方法获取函数的参数。参考资料详解PHP反射的基本使用反射在PHP中的应用手册