我們都知道,類別的私有屬性在類別外部是無法存取的,包括子類別中也是不可存取的。例如如下程式碼:
<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教學有興趣的朋友有幫助。