If you have an object, you can access its member properties through array mode and object mode; 100 min

Source: Internet
Author: User
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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.