合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 反射机制(补充部分内容) ### 反射返回的是一个类 ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">?</span>php namespace app\index\controller<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Index</span> <span class="token1">{</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">index</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token6">// var_dump(\A::class);// string(1) "A" object(A)#36 (0) { }</span> $obj <span class="token">=</span> <span class="token2">new</span> <span class="token3"><span class="token1">\</span>A</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// var_dump($obj); // object(A)#34 (0) { }</span> $obj1 <span class="token">=</span> <span class="token2">new</span> <span class="token3"><span class="token1">\</span>ReflectionClass</span><span class="token1">(</span>$obj<span class="token1">)</span><span class="token1">;</span> <span class="token6">// var_dump($obj1); // object(ReflectionClass)#35 (1) { ["name"]=> string(1) "A" }</span> <span class="token3">var_dump</span><span class="token1">(</span>$obj1<span class="token">-</span><span class="token">></span><span class="token3">newInstance</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span><span class="token6">// object(A)#36 (0) { }</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` ### 反射机制 ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">?</span>php namespace app\index\controller<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Index</span> <span class="token1">{</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">index</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $obj <span class="token">=</span> <span class="token2">new</span> <span class="token3"><span class="token1">\</span>A</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// var_dump($obj); // 输出一个A类的对象</span> <span class="token6">//建立A类的一个反射类</span> $obj2 <span class="token">=</span><span class="token2">new</span> <span class="token3"><span class="token1">\</span>ReflectionClass</span><span class="token1">(</span>$obj<span class="token1">)</span><span class="token1">;</span> <span class="token6">// var_dump($obj2);</span> <span class="token6">/** * 上面返回 * object(ReflectionClass)#35 (1) { ["name"]=> string(1) "A" } */</span> $instance <span class="token">=</span> $obj2<span class="token">-</span><span class="token">></span><span class="token3">newInstance</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span><span class="token6">// 相当于实例化了这个类 ReflectionClass::newInstance — 从指定的参数创建一个新的类实例</span> <span class="token6">// var_dump($instance); // 返回 一个对象 和最开始返回的一样</span> <span class="token6">// 获取实例化对象的所有方法 ReflectionClass::getMethods — 获取方法的数组</span> $<span class="token5">M</span> <span class="token">=</span> $obj2<span class="token">-</span><span class="token">></span><span class="token3">getMethods</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// var_dump($M);</span> <span class="token6">/** * 输出 * array(2) { [0]=> object(ReflectionMethod)#37 (2) { ["name"]=> string(3) "aaa" ["class"]=> string(1) "A" } [1]=> object(ReflectionMethod)#38 (2) { ["name"]=> string(3) "bbb" ["class"]=> string(1) "A" } } */</span> <span class="token6">// 获取所有注释</span> <span class="token3">foreach</span> <span class="token1">(</span>$<span class="token5">M</span> <span class="token2">as</span> $method<span class="token1">)</span><span class="token1">{</span> <span class="token3">var_dump</span><span class="token1">(</span>$method<span class="token">-</span><span class="token">></span><span class="token3">getDocComment</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token6">/** *就会返回string(39) "/** * @return a的注释 */</span><span class="token6">/** * @return 这是b的注释 */</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` ### 调用方法 ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">?</span>php namespace app\index\controller<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Index</span> <span class="token1">{</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">index</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $obj <span class="token">=</span> <span class="token2">new</span> <span class="token3"><span class="token1">\</span>A</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> $obj2 <span class="token">=</span> <span class="token2">new</span> <span class="token3"><span class="token1">\</span>ReflectionClass</span><span class="token1">(</span>$obj<span class="token1">)</span><span class="token1">;</span> <span class="token6">// 方法一</span> $instance <span class="token">=</span> $obj2<span class="token">-</span><span class="token">></span><span class="token3">newInstance</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// var_dump($instance->aaa(1,2));</span> <span class="token6">// 方法二</span> $method <span class="token">=</span> $obj2<span class="token">-</span><span class="token">></span><span class="token3">getMethod</span><span class="token1">(</span><span class="token4">'aaa'</span><span class="token1">)</span><span class="token1">;</span> echo $method<span class="token">-</span><span class="token">></span><span class="token3">invokeArgs</span><span class="token1">(</span>$instance<span class="token1">,</span><span class="token1">[</span><span class="token4">'mm'</span><span class="token1">,</span><span class="token4">'n'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` ### 判断某个方法是公用还是私有 ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">?</span>php namespace app\index\controller<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Index</span> <span class="token1">{</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">index</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $obj <span class="token">=</span> <span class="token2">new</span> <span class="token3"><span class="token1">\</span>A</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> $obj2 <span class="token">=</span> <span class="token2">new</span> <span class="token3"><span class="token1">\</span>ReflectionClass</span><span class="token1">(</span>$obj<span class="token1">)</span><span class="token1">;</span> <span class="token6">// 方法一</span> $instance <span class="token">=</span> $obj2<span class="token">-</span><span class="token">></span><span class="token3">newInstance</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// 相当于实例化这个类</span> <span class="token6">// var_dump($instance->aaa(1,2));</span> <span class="token6">// 方法二</span> <span class="token6">//$method = $obj2->getMethod('aaa');</span> <span class="token6">// echo $method->invokeArgs($instance,['mm','n']);</span> <span class="token6">// 方法三 只能调用不带参数的</span> <span class="token6">// $method = $obj2->getMethod('bbb');</span> <span class="token6">// echo $method->invoke($instance);</span> <span class="token6">// 判断某个方法是私有还是公共的 注意要传入obj</span> $method <span class="token">=</span> <span class="token2">new</span> <span class="token3"><span class="token1">\</span>ReflectionMethod</span><span class="token1">(</span>$obj<span class="token1">,</span><span class="token4">'bbb'</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">var_dump</span><span class="token1">(</span>$method<span class="token">-</span><span class="token">></span><span class="token3">isPublic</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` ### 反射机制补充 ``` <pre class="calibre14">``` <span class="token"><</span><span class="token">?</span>php namespace app\index\controller<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Index</span> <span class="token1">{</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">index</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token6">// var_dump(\A::class);// string(1) "A" object(A)#36 (0) { }</span> <span class="token6">//$obj = new \A();</span> <span class="token6">// var_dump($obj); // object(A)#34 (0) { }</span> <span class="token6">//$obj1 = new \ReflectionClass($obj);</span> <span class="token6">//var_dump($obj1); // object(ReflectionClass)#35 (1) { ["name"]=> string(1) "A" }</span> <span class="token6">// var_dump($obj1->newInstance());// object(A)#36 (0) { }</span> <span class="token6">//$obj2 = new \ReflectionClass('\A');</span> <span class="token6">//var_dump($obj2); //object(ReflectionClass)#34 (1) { ["name"]=> string(1) "A" }</span> $obj3 <span class="token">=</span> <span class="token2">new</span> <span class="token3"><span class="token1">\</span>ReflectionClass</span><span class="token1">(</span><span class="token4">'A'</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">var_dump</span><span class="token1">(</span>$obj3<span class="token1">)</span><span class="token1">;</span> <span class="token6">// object(ReflectionClass)#34 (1) { ["name"]=> string(1) "A" }</span> <span class="token6">/** * @ yue 创建新的反射类的时候参数可为包含类名的字符串 或者 类的对象 * 结果都是一样的 如上图所示 */</span> <span class="token1">}</span> <span class="token1">}</span> ``` ```