Heim >Backend-Entwicklung >PHP-Tutorial >So greifen Sie in PHP von außerhalb einer Klasse auf private Eigenschaften einer Klasse zu
Wir alle wissen, dass auf die privaten Eigenschaften einer Klasse außerhalb der Klasse nicht zugegriffen werden kann, auch nicht in Unterklassen. Zum Beispiel der folgende 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(); 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>Aber in einigen Fällen müssen wir auf die privaten Eigenschaften der Klasse zugreifen. Es gibt mehrere Möglichkeiten, dies zu erreichen:
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. Verwenden Sie Closure::bind()
Diese Methode ist neu in 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>Dementsprechend können wir eine Funktion kapseln zum Lesen/Festlegen der privaten Attribute der Klasse:
<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() ist auch sehr nützlich. Wir können diese Funktion verwenden, um Methoden dynamisch zu einer Klasse hinzuzufügen. Die offizielle Dokumentation gibt ein Beispiel:
<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>Das Obige stellt die Methode des externen Zugriffs auf die privaten Eigenschaften einer Klasse in PHP vor, einschließlich des relevanten Inhalts. Ich hoffe, dass es für Freunde hilfreich ist, die sich für PHP-Tutorials interessieren.