2

配列要素をマスター配列に格納および取得するために__get()、を使用するクラスを作成していました。__set()基本的にプライベートプロパティを再作成するために、いくつかの要素を取得できないようにするためのチェックを行いました。

__getクラスプロパティへのすべての呼び出しをインターセプトしているように見えることに気づきました。外の世界にプライベートな変数(getを介して利用できない)が欲しかったので、これは私にとってはひどいですが、クラス内からマスター配列を直接参照することによってそれにアクセスしようとしていました。もちろん、マスター配列はgettableプロパティのホワイトリストに含まれていません:(

とを使用するphpクラスでパブリックプロパティとプライベートプロパティをエミュレートする方法は__get()あり__set()ますか?

例:

<?
abstract class abstraction {

    private $arrSettables;
    private $arrGettables;
    private $arrPropertyValues;
    private $arrProperties;

    private $blnExists = FALSE;

    public function __construct( $arrPropertyValues, $arrSettables, $arrGettables ) {

        $this->arrProperties = array_keys($arrPropertyValues);
        $this->arrPropertyValues = $arrPropertyValues;
        $this->arrSettables = $arrSettables;
        $this->arrGettables = $arrGettables;
    }

    public function __get( $var ) {
        echo "__get()ing:\n";
        if ( ! in_array($var, $this->arrGettables) ) {
            throw new Exception("$var is not accessible.");
        }

        return $this->arrPropertyValues[$var];
    }

    public function __set( $val, $var ) {
        echo "__set()ing:\n";
        if ( ! in_array($this->arrSettables, $var) ) {
            throw new Exception("$var is not settable.");
        }

        return $this->arrPropertyValues[$var];
    }

} // end class declaration

class concrete extends abstraction {

    public function __construct( $arrPropertyValues, $arrSettables, $arrGettables ) {
        parent::__construct( $arrPropertyValues, $arrSettables, $arrGettables );
    }

    public function runTest() {

        echo "Accessing array directly:\n";
        $this->arrPropertyValues['color'] = "red";
        echo "Color is {$this->arrPropertyValues['color']}.\n";

        echo "Referencing property:\n";
        echo "Color is {$this->color}.\n";
        $this->color = "blue";
        echo "Color is {$this->color}.\n";

        $rand = "a" . mt_rand(0,10000000);
        $this->$rand = "Here is a random value";
        echo "'$rand' is {$this->$rand}.\n";

    }
}

try {
    $objBlock = & new concrete( array("color"=>"green"), array("color"),  array("color") );
    $objBlock->runTest();
} catch ( exception $e ) {
    echo "Caught Exeption $e./n/n";
}

// no terminating delimiter

$ php test.php
Accessing array directly:
__get()ing:
Caught Exeption exception 'Exception' with message 'arrPropertyValues is not accessible.' in /var/www/test.php:23
Stack trace:
#0 /var/www/test.php(50): abstraction->__get('arrPropertyValu...')
#1 /var/www//test.php(68): concrete->runTest()
#2 {main}.
4

3 に答える 3

2

__get() と __set() を使用する php クラスでパブリック プロパティとプライベート プロパティをエミュレートする方法はありますか?

直接ではありません(割引する場合debug_backtrace)。

getPrivただし、現在行っているすべての作業を行うプライベート メソッドを使用できます__get。次に__get、このプライベート メソッドのみをラップし、アクセシビリティをチェックします。

function __get($name) {        
    if (in_array($name, $this->privateProperties))
        throw new Exception("The property ". __CLASS__ . "::$name is private.");
    return $this->getPriv($name);
}

クラス内では、 を呼び出しgetPrivて、 をバイパスし__getます。

于 2010-08-13T17:00:44.780 に答える
1

abstraction::$arrPropertyValues保護する必要がある場合を除いて、保護するか、Artefacto が記述したことを行います (追加のチェックが必要な場合) abstraction::getPriv()

于 2010-08-13T17:19:48.463 に答える
1

プライベート/保護されたプロパティを手動で登録するのではなく、PHP の面倒なリフレクション メソッドを使用できます。

function __get($name) {

    $reflect = new ReflectionObject($this);
    $publics = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);

    if (in_array($name, $publics)) {
         return $this->{$name};
    }
}
于 2010-08-13T17:37:52.293 に答える