我们都知道,类的私有属性在类外部是不可访问的,包括子类中也是不可访问的。比如如下代码:
<code><span><span><?php </span> <span>class</span> Example1{ <span>private</span> <span>$</span><span>_prop</span> = <span>'test'</span>; } <span>$</span><span>r</span> = <span>function</span>(Example1 <span>$</span><span>e</span>){ <span>return</span> <span>$</span><span>e</span>->_prop; }; <span>$</span><span>a</span> = <span>new</span> Example1(); var_dump(<span>$</span><span>r</span>(<span>$</span><span>a</span>)); <span>//运行结果:Fatal error: Cannot access private property Example1::$_prop</span> <span>?></span> </span></span></code>
1.利用反射
<code><span><span><?php </span> <span>class</span> Example1{ <span>private</span> <span>$</span><span>_prop</span> = <span>'test'</span>; } <span>$</span><span>r</span> = <span>function</span>(Example1 <span>$</span><span>e</span>){ <span>return</span> <span>$</span><span>e</span>->_prop; }; <span>$</span><span>a</span> = <span>new</span> Example1(); <span>$</span><span>rfp</span> = <span>new</span> ReflectionProperty(<span>'Example1'</span>,<span>'_prop'</span>); <span>$</span><span>rfp</span>->setAccessible(<span>true</span>); var_dump(<span>$</span><span>rfp</span>->getValue(<span>$</span><span>a</span>)); <span>//结果输出:string 'test' (length=4)</span> <span>?></span> </span></span></code>
2.利用Closure::bind()
此方法是php 5.4.0中新增的。
<code><span><span><?php </span> <span>class</span> Example1{ <span>private</span> <span>$</span><span>_prop</span> = <span>'test'</span>; } <span>$</span><span>r</span> = <span>function</span>(Example1 <span>$</span><span>e</span>){ <span>return</span> <span>$</span><span>e</span>->_prop; }; <span>$</span><span>a</span> = <span>new</span> Example1(); <span>$</span><span>r</span> = Closure::bind(<span>$</span><span>r</span>,<span>null</span>,<span>$</span><span>a</span>); var_dump(<span>$</span><span>r</span>(<span>$</span><span>a</span>)); <span>//结果输出:string 'test' (length=4)</span> <span>?></span> </span></span></code>
<code><span><span><?php </span> <span>class</span> Example1{ <span>private</span> <span>$</span><span>_prop</span> = <span>'test'</span>; } <span>$</span><span>a</span> = <span>new</span> Example1(); <span>$</span><span>r</span> = Closure::bind(<span>function</span> & (Example1 <span>$</span><span>e</span>) { <span>return</span> <span>$</span><span>e</span>->_prop; }, <span>null</span>, <span>$</span><span>a</span>); <span>$</span><span>cake</span> = & <span>$</span><span>r</span>(<span>$</span><span>a</span>); <span>$</span><span>cake</span> = <span>'lie'</span>; var_dump(<span>$</span><span>r</span>(<span>$</span><span>a</span>)); <span>//结果输出:string 'lie' (length=3)</span> </span></span></code>
<code><span><span><?php </span> <span>$</span><span>reader</span> = <span>function</span> & (<span>$</span><span>object</span>, <span>$</span><span>property</span>) { <span>$</span><span>value</span> = & Closure::bind(<span>function</span> & () <span>use</span> (<span>$</span><span>property</span>) { <span>return</span> <span>$</span><span>this</span>-><span>$</span><span>property</span>; }, <span>$</span><span>object</span>, <span>$</span><span>object</span>)->__invoke(); <span>return</span> <span>$</span><span>value</span>; }; <span>?></span> </span></span></code>
Closure::bind()还有一个很有用之处,我们可以利用这一特性来给一个类动态的添加方法。官方文档中给了这么一个例子:
<code><span><span><?php </span> trait MetaTrait { <span>private</span> <span>$</span><span>methods</span> = <span>array</span>(); <span>public</span> <span>function</span> addMethod(<span>$</span><span>methodName</span>, <span>$</span><span>methodCallable</span>) { <span>if</span> (!is_callable(<span>$</span><span>methodCallable</span>)) { <span>throw</span> <span>new</span> InvalidArgumentException(<span>'Second param must be callable'</span>); } <span>$</span><span>this</span>->methods[<span>$</span><span>methodName</span>] = Closure::bind(<span>$</span><span>methodCallable</span>, <span>$</span><span>this</span>, get_class()); } <span>public</span> <span>function</span> __call(<span>$</span><span>methodName</span>, <span>array</span> <span>$</span><span>args</span>) { <span>if</span> (<span>isset</span>(<span>$</span><span>this</span>->methods[<span>$</span><span>methodName</span>])) { <span>return</span> call_user_func_array(<span>$</span><span>this</span>->methods[<span>$</span><span>methodName</span>], <span>$</span><span>args</span>); } <span>throw</span> RunTimeException(<span>'There is no method with the given name to call'</span>); } } <span>class</span> HackThursday { <span>use</span> MetaTrait; <span>private</span> <span>$</span><span>dayOfWeek</span> = <span>'Thursday'</span>; } <span>$</span><span>test</span> = <span>new</span> HackThursday(); <span>$</span><span>test</span>->addMethod(<span>"addedMethod"</span>,<span>function</span>(){ <span>return</span> <span>'我是被动态添加进来的方法'</span>; }); <span>echo</span> <span>$</span><span>test</span>->addedMethod(); <span>//结果输出:我是被动态添加进来的方法</span> <span>?></span></span></span></code>
以上就介绍了php中类外部访问类私有属性的方法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。