PHP object like array

后端 未结 9 564
臣服心动
臣服心动 2020-12-10 00:53

I need to be able to set my object like this:

$obj->foo = \'bar\';

then I need to use it as an array like that:

if($obj[\'f         


        
9条回答
  •  误落风尘
    2020-12-10 01:41

    Try extending ArrayObject

    You'll also need to implement a __get Magic Method as Valentin Golev mentioned.

    Your class will need to looks something like this:

    Class myClass extends ArrayObject {
        // class property definitions...
        public function __construct()
        {
            //Do Stuff
        }
    
        public function __get($n) { return $this[$n]; }
    
        // Other methods
    }
    

提交回复
热议问题