如果在未做任何处理的情况下, 以数组的方式访问对象,会抛给你一个大大的错误。
Fatal error: Uncaught Error: Cannot use object of type Test as array
登录后复制
当然如果你对类进行一些改造的话,还是可以像数组一样访问。
如何访问受保护的对象属性
在正式改造之前,先看另一个问题。当我们试图访问一个受保护的属性的时候,也会抛出一个大大的错误。
Fatal error: Uncaught Error: Cannot access private property Test::$container
登录后复制
是不是受保护属性就不能获取?当然不是,如果我们想要获取受保护的属性,我们可以借助魔术方法__get。
立即学习“PHP免费学习笔记(深入)”;
相关推荐:《php数组》
DEMO1
获取私有属性
container = ['one'=>1, 'two'=>2, 'three'=>3]; } public function __get($name) { return property_exists($this, $name) ? $this->$name : null; }}$test = new Test();var_dump($test->container);
登录后复制
DEMO2
获取私有属性下对应键名的键值。
container = ['one'=>1, 'two'=>2, 'three'=>3]; } public function __get($name) { return array_key_exists($name, $this->container) ? $this->container[$name] : null; } }$test = new Test();var_dump($test->one);
登录后复制
如何以数组的方式访问对象
我们需要借助预定义接口中的ArrayAccess接口来实现。接口中有4个抽象方法,需要我们实现。
container = ['one'=>1, 'two'=>2, 'three'=>3]; } public function offsetExists($offset) { return isset($this->container[$offset]); } public function offsetGet($offset){ return isset($this->container[$offset]) ? $this->container[$offset] : null; } public function offsetSet($offset, $value) { if(is_null($offset)){ $this->container[] = $value; }else{ $this->container[$offset] = $value; } } public function offsetUnset($offset){ unset($this->container[$offset]); } }$test = new Test();var_dump($test['one']);
登录后复制
如何遍历对象
其实对象在不做任何处理的情况下,也可以被遍历,但是只能遍历可见属性,也就是定义为public的属性。我们可以借助另一个预定义接口IteratorAggregate,来实现更为可控的对象遍历。
container = ['one'=>1, 'two'=>2, 'three'=>3]; } public function getIterator() { return new ArrayIterator($this->container); } }$test = new Test();foreach ($test as $k => $v) { var_dump($k, $v);}
登录后复制
以上就是php数组中对象如何访问的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2525071.html