2

我正在编写一个类,它使用__get()__set()在主数组中存储和检索数组元素。我进行了检查以使某些元素变得难以捉摸,基本上是为了重新创建私有属性。

我注意到它似乎__get拦截了对类属性的所有调用。这对我来说很糟糕,因为我希望有一个外部世界私有的变量(通过 get 不可用),但我试图通过直接从类中引用主数组来访问它。当然,主数组不在可获取属性的白名单中:(

有没有办法可以在使用__get()and的 php 类中模拟公共和私有属性__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::$arrPropertyValuesArtefacto 编写的操作(如果您需要额外的检查),但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 回答