Home >Backend Development >PHP Tutorial >How to access private properties of a class from outside a class in PHP

How to access private properties of a class from outside a class in PHP

WBOY
WBOYOriginal
2016-08-08 09:32:181563browse

We all know that the private properties of a class are not accessible outside the class, including in subclasses. For example, the following 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>
But in some cases we need to access the private properties of the class. There are several methods to achieve this:

1. Use reflection

<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. Use Closure::bind()
This The method is new 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>
In addition, we can also access by reference, so that we can modify the private properties of the class:
<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>

According to this, we can encapsulate a function to read/set the private properties of the class:
<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() is also very useful. We can use this feature to dynamically add methods to a class. The official documentation gives an example:

<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>

The above introduces the method of externally accessing the private properties of a class in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn