PHP反射原理与用法深入分析
php  /  管理员 发布于 7年前   163
本文实例讲述了PHP反射原理与用法。分享给大家供大家参考,具体如下: 说到反射,实际上包含两个概念: PHP官方文档写得很清晰了,下面我就说一下具体的应用。 1.参数检测 有时候需要在函数里需要判断传入的参数类型是否合法。 2.动态调用 在依赖注入中,常见到这种用法,比如Laravel5.5中的Container.php 上述代码先判断是否是闭包,如果是,直接返回。不是则通过 生成反射类的实例,然后获取这个类的构造函数和参数,进行初始化的过程。 注意 反射里一个比较重要的用法invoke 当已知这个类的时候,可以通过构造ReflectionMethod来直接调用,如: 当不知道这个类时,知道类的对象,可以用ReflectionObject获取ReflectionMethod后调用,如: 调用流程一般就是获取反射类ReflectionClass/反射对象ReflectionObject的实例,然后获取ReflectionMethod后,invoke。 3.获取注释,生成文档 比如PHPDoc 4.注解,增强版的注释,符合一定的规则 比如某些框架的路由,便是通过注解实现的。 5.不要为了反射而反射 PHP是一门动态语言,其实可以直接通过字符串来调用类或函数,如下: 那么为什么还需要反射呢? 更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》 希望本文所述对大家PHP程序设计有所帮助。
这时可以使用is_a、is_subclass_of来检测。或者结合反射,做更多检测。public function build($concrete) { // If the concrete type is actually a Closure, we will just execute it and // hand back the results of the functions, which allows functions to be // used as resolvers for more fine-tuned resolution of these objects. if ($concrete instanceof Closure) { return $concrete($this, $this->getLastParameterOverride()); } $reflector = new ReflectionClass($concrete); // If the type is not instantiable, the developer is attempting to resolve // an abstract type such as an Interface of Abstract Class and there is // no binding registered for the abstractions so we need to bail out. if (! $reflector->isInstantiable()) { return $this->notInstantiable($concrete); } $this->buildStack[] = $concrete; $constructor = $reflector->getConstructor(); // If there are no constructors, that means there are no dependencies then // we can just resolve the instances of the objects right away, without // resolving any other types or dependencies out of these containers. if (is_null($constructor)) { array_pop($this->buildStack); return new $concrete; } $dependencies = $constructor->getParameters(); // Once we have all the constructor's parameters we can create each of the // dependency instances and then use the reflection instances to make a // new instance of this class, injecting the created dependencies in. $instances = $this->resolveDependencies( $dependencies ); array_pop($this->buildStack); return $reflector->newInstanceArgs($instances); }
new ReflectionClass($concrete);
class HelloWorld { public function sayHelloTo($name) { return 'Hello ' . $name; }}$reflectionMethod = new ReflectionMethod('HelloWorld', 'sayHelloTo');echo $reflectionMethod->invoke(new HelloWorld(), 'Mike');
class HelloWorld { public function sayHelloTo($name) { return 'Hello ' . $name; }}$hello = new HelloWorld();$refObj = new ReflectionObject($hello);$refMethod = $refObj->getMethod('sayHelloTo');echo $refMethod->invoke($hello,'Mike');
class HelloWorld { public function sayHelloTo($name) { return 'Hello ' . $name; }}$hello = 'HelloWorld';$helloSay = 'sayHelloTo';$helloIntance = new $hello;echo $helloIntance->$helloSay('Mike');
您可能感兴趣的文章:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号