🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 五、反射的应用场景 ``` <pre class="calibre29">``` class <span class="token4">Test</span> <span class="token3">{</span> <span class="token5">function</span> <span class="token4">phone</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> <span class="token2">'13888888888'</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">function</span> <span class="token4">user</span><span class="token3">(</span>$name<span class="token3">,</span>$sex<span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> <span class="token2">'我是'</span><span class="token3">.</span>$name<span class="token3">.</span><span class="token2">',性别'</span><span class="token3">.</span>$sex<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> $obj <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Test</span><span class="token3">;</span> <span class="token">//调用phone方法</span> $med <span class="token1">=</span> <span class="token5">new</span> <span class="token4">ReflectionMethod</span><span class="token3">(</span>$obj<span class="token3">,</span><span class="token2">'phone'</span><span class="token3">)</span><span class="token3">;</span> echo $med<span class="token1">-</span><span class="token1">></span><span class="token4">invoke</span><span class="token3">(</span>$obj<span class="token3">)</span><span class="token3">;</span> echo <span class="token2">'<hr/>'</span><span class="token3">;</span> <span class="token">//调用带参数的user方法</span> $med2 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">ReflectionMethod</span><span class="token3">(</span>$obj<span class="token3">,</span><span class="token2">'user'</span><span class="token3">)</span><span class="token3">;</span> echo $med2<span class="token1">-</span><span class="token1">></span><span class="token4">invokeArgs</span><span class="token3">(</span>$obj<span class="token3">,</span><span class="token4">array</span><span class="token3">(</span><span class="token2">'lws'</span><span class="token3">,</span><span class="token2">'男'</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> ``` ```