访问受保护的变量键和值


Accessing protected variable keys and values

有没有办法访问哈希没有循环?如果没有,还有其他最简单的方法吗?变量来自Symfony2 createQueryBuilder...->...->getResult();

echo '<pre>'; print_r($var);
Array
(
    [0] => User'RecordBundle'Entity'UserEntity Object
        (
            [id:protected] => 5
            [fullname:protected] => yoyo
            [email:protected] => yoyo@yoyo.com
            [username:protected] => yoyoyoy
            [password:protected] => 
            [confirmation:protected] => 
            [tc:protected] => 
            [hash:protected] => bb53ed2057d377bab37839ba0d66091ada5c525f
            [salt:protected] => aeff8de4e3a3b28412b27cb02932547ca7e56249
            [created:protected] => DateTime Object
                (
                    [date] => 2014-06-10 21:29:59
                    [timezone_type] => 3
                    [timezone] => Europe/Ja
                )
            [updated:protected] => 
        )
)

假定UserEntity类为其属性定义了getter和setter,因此要访问属性hash,您可以执行如下操作

$userObject = $var[0];
$var[0]->getHash();

其中getHash()定义为

class UserEntity {
    protected $hash;
    public function getHash() {
        return $this->hash;
    }
}