像访问数组一样访问对象

来源:互联网 发布:r11支持5g网络吗 编辑:程序博客网 时间:2024/05/22 12:19
class A{}$a = new A;$a['a'];

但是这样访问是会出错的

Fatal error: Cannot use object of type A as array in

在 www.php.net/arrayaccess 里有详细的介绍

arrayaccess 是内置的一个接口  有四个抽象方法  offsetExists($offset) 、offsetGet($offset)、offsetSet($offset,$value)、offsetUnset($offset)

所以要在 类里重载   

class A implements ArrayAccess{function offsetExists($offset){}function offsetGet($offset){}function offsetSet($offset,$value){}function offsetUnset($offset){}}


原创粉丝点击