php中類別外部存取類別私有屬性的方法

WBOY
發布: 2016-08-08 09:32:18
原創
1536 人瀏覽過

我們都知道,類別的私有屬性在類別外部是無法存取的,包括子類別中也是不可存取的。例如如下程式碼:

<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教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板