我正在尝试获取一个数组的属性,该数组包含在一个对象中,而该对象本身包含在另一个对象。
下面的代码是var_dump
ed对象。我正在尝试从属性identity中获取值,即'identity'=>int 101。。
我将'identity'=>101放在星号中,仅用于演示目的;但是所呈现的值没有星号。这是代码最后一行的第4行。
我知道如何从多维数组中获取值;但不确定多维对象
object(ZfcUser'Authentication'Adapter'AdapterChain)[373]
protected 'event' =>
object(ZfcUser'Authentication'Adapter'AdapterChainEvent)[456]
protected 'name' => string 'authenticate.success' (length=20)
protected 'target' =>
&object(ZfcUser'Authentication'Adapter'AdapterChain)[373]
protected 'params' =>
array (size=4)
'request' =>
object(Zend'Http'PhpEnvironment'Request)[194]
...
**'identity' => int 101**
'code' => int 1
'messages' =>
array (size=1)
要访问对象的属性,使用的语法为->
,如object->property
中所示。要访问给定的属性,它的可见性必须是public
,而您的示例中的属性具有protected
可见性。
要访问identity
,您需要运行$object->event->params['identity']
,还需要定义一个getter函数或将属性的可见性更改为public。
吸气剂函数的一个例子
public function __get($name){
return $this->$name;
}
或者对于单一属性
public function getEvent(){
return $this->event;
}
然后使用的语法将是(假设您也为params
定义了一个getter函数)
$object->getEvent()->getProperties()['identity'];