PHP中的ArrayAccess用法
来源:互联网 发布:atheros无线网卡 linux 编辑:程序博客网 时间:2024/06/13 22:37
最近看laravel源码,发现里面用了很多框架类实现了ArrayAccess接口,以前对这块不是很熟悉,查了一下这个语法的用法,发现这个其实就是实现让对象以数组形式来使用。
在官方文档上:
ArrayAccess {/* Methods */abstract public boolean offsetExists ( mixed $offset )abstract public mixed offsetGet ( mixed $offset )abstract public void offsetSet ( mixed $offset , mixed $value )abstract public void offsetUnset ( mixed $offset )}
实现上面的方法,下面举个实例
<?php/** * Created by PhpStorm. * User: wangHan * Date: 2016/10/21 * Time: 14:07 */class Human implements ArrayAccess{ private $elements; public function __construct() { $this->elements = [ "boy" => "male", "girl" => "female" ]; } public function offsetExists($offset) { // TODO: Implement offsetExists() method. return isset($this->elements[$offset]); } public function offsetGet($offset) { // TODO: Implement offsetGet() method. return $this->elements[$offset]; } public function offsetSet($offset, $value) { // TODO: Implement offsetSet() method. $this->elements[$offset] = $value; } public function offsetUnset($offset) { // TODO: Implement offsetUnset() method. unset($this->elements[$offset]); }}$human = new Human();$human['people'] = "boyAndGirl"; ////自动调用offsetSetif(isset($human['people'])) { ////自动调用offsetExists echo $human['boy'];//自动调用offsetGet echo '<br />'; unset($human['boy']);//自动调用offsetUnset var_dump($human['boy']);}// // 输出结果 male null
引申下,配合单例使用才好!可以在自己的框架中使用。如下
<?php//Configuration Classclass Configuration implements ArrayAccess { static private $config; private $configarray; private function __construct() { $this->configarray = array( "Wang" => "Male", "Han" => "Female" ); } public static function instance() { if (self::$config == null) { self::$config = new Configuration(); } return self::$config; } function offsetExists($index) { return isset($this->configarray[$index]); } function offsetGet($index) { return $this->configarray[$index]; } function offsetSet($index, $newvalue) { $this->configarray[$index] = $newvalue; } function offsetUnset($index) { unset($this->configarray[$index]); }} $config = Configuration::instance();print $config["Wang"];//正如你所预料的,程序的输出是"Male"。//假如我们做下面那样的动作:$config = Configuration::instance();print $config["Han"];$config['Han'] = "Wang's Lover";// config $configTest = Configuration::instance();print $configTest ['Han']; //是的,也正如预料的,输出的将是Wang's Lover
0 0
- PHP中的ArrayAccess用法
- PHP中的Iterator、ArrayAccess、Countable接口
- PHP - ArrayAccess接口详解
- PHP ArrayAccess接口
- 231 php ArrayAccess
- PHP的ArrayAccess学习笔记
- PHP的ArrayAccess接口简介
- PHP预定义接口ArrayAccess
- php的内置接口 arrayaccess
- PHP之ArrayAccess接口实例代码介绍
- PHP ArrayAccess(数组式访问)接口
- PHP数组式访问接口 ArrayAccess
- PHP预定义接口之 ArrayAccess
- php数组式访问借口ArrayAccess
- PHP预定义接口之 ArrayAccess
- PHP预定义接口之ArrayAccess
- 24.php中使用Iterator,ArrayAccess和Countable
- Magento 用了 php 中 ArrayAccess,IteratorAggregate,Countable
- Android屏幕适配
- 随笔 - 字符串常用方法总结
- C/C++调用约定
- 解决IE浏览器,img的src加载图片问题
- 警告@SuppressWarnings("serial")与对象序列化(Java IO)
- PHP中的ArrayAccess用法
- 你所不知道的Activity转场动画——ActivityOptions
- pip安装python包报错的有效解决方法
- js中的逻辑与(&&)和逻辑或(||)问题
- AngularJS学习笔记之directive——scope选项与绑定策略(*******)
- iOS7 以下获取网络状态
- 购物车类 获取总金额
- 将list集合,元素随机打乱
- 联合容器