PHP object like array

后端 未结 9 552
臣服心动
臣服心动 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:42

    Just add implements ArrayAccess to your class and add the required methods:

    • public function offsetExists($offset)
    • public function offsetGet($offset)
    • public function offsetSet($offset, $value)
    • public function offsetUnset($offset)

    See http://php.net/manual/en/class.arrayaccess.php

提交回复
热议问题