For an object, you can access its member attributes in array mode and object mode; 100 points: class {
Public $ aaa = 1;
}
$ A = new ();
Echo $ a-> aaa; // 1
At the same time, you can directly:
Echo $ a ['AAA']; // 1
In PDO, we can implement $ db-> fetch (PDO: FETCH_LAZY). This shows that PHP can achieve the above results. how can we implement it?
Thank you for your consideration;
Reply to discussion (solution)
b = 1;$a['b'] = 2;echo $a->b;echo $a['b'];
ArrayObject can be inherited, but sub-classes are not allowed to have methods. For more information, see the manual...
I don't think it makes sense to say anything out of the box...
You need to write a method implementation.
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