class a{
public $aaa=1;
}
$a=new a();
echo $a->aaa; //1
同时可以直接:
echo $a['aaa']; //1
在PDO中,可以 $db->fetch(PDO::FETCH_LAZY)来实现,这可以说明,PHP是可以做到上述结果的; 请问具体是如何实现的?
求原理,谢谢;
<?php$a = new ArrayObject();$a->b = 1;$a['b'] = 2;echo $a->b;echo $a['b'];
要写个方法实现。
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