Class a{
public $aaa = 1;
}
$a =new A ();
Echo $a->aaa; 1
You can also directly:
echo $a [' AAA ']; 1
In PDO, it can be implemented $db->fetch (Pdo::fetch_lazy), which shows that PHP can do the above results; How is the concrete realization?
Principle, thank you;
Reply to discussion (solution)
b = 1; $a [' b '] = 2;echo $a->b;echo $a [' B '];
Arrayobject can inherit, but do not allow subclasses have methods, specific look at the manual I do not know ...
I don't think this thing means anything.
To write a method to implement.
Class a{public $aaa =1; function Fetch ($type) { if ($type = = ' Assoc ') { $ret = new StdClass (); $ret->aaa = $this->aaa; } ElseIf ($type = = ' array ') { $ret = array (); $ret [' aaa '] = $this->aaa; } return $ret; }} $d = new A (), $obj = $d->fetch (' Assoc '), Var_dump ($obj), $arr = $d->fetch (' array '); Var_dump ($arr);
Object (StdClass) [2] public ' aaa ' = + int 1array (size=1) ' aaa ' = int 1