クラスのプライベート プロパティには、サブクラスを含め、クラスの外部からはアクセスできないことは誰もが知っています。たとえば、次のコード:
<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. クロージャを使用します。 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>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>
<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>
上記では、PHP のクラスのプライベート プロパティに外部からアクセスする方法を、関連する内容も含めて紹介しました。PHP チュートリアルに興味のある友人の役に立てば幸いです。