php的ArrayAccess使用
SPL提供了一组标准数据结构,php5.3以后成为了PHP内核的一部分,不需要单独安装任何模块,可以直接使用。比如用于观察者模式的Subject接口,这里的ArrayAccess也属于SPL标准库
官方介绍:https://www.php.net/manual/en/class.arrayaccess.php
ArrayAccess的用处:以数组的方式访问对象
使用很简单,4个接口方法:
ArrayAccess {
/* Methods */
public offsetExists ( mixed $offset ) : bool //查看指定设置是否存在
public offsetGet ( mixed $offset ) : mixed //获取指定的设置
public offsetSet ( mixed $offset , mixed $value ) : void //设置某个值
public offsetUnset ( mixed $offset ) : void //取消指定的设置
}简单使用:
<?php
class obj implements ArrayAccess {
private $container = array();
public function offsetSet($offset, $value) {
echo '调用' . __METHOD__ . '方法' . PHP_EOL;
$this->container[$offset] = $value;
}
public function offsetExists($offset) {
echo '调用' . __METHOD__ . '方法' . PHP_EOL;
return isset($this->container[$offset]);
}
public function offsetUnset($offset) {
echo '调用' . __METHOD__ . '方法' . PHP_EOL;
unset($this->container[$offset]);
}
public function offsetGet($offset) {
echo '调用' . __METHOD__ . '方法' . PHP_EOL;
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
}
//实例化
$obj = new obj;
//赋值key:value
$obj['name'] = '李小龙'; //调用offsetSet 方法 相当于$obj->offsetSet('name','李小龙')
//取值
$obj['name']; //调用offsetGet方法,相当于$obj->offsetGet('name')
//检查是否存在
isset($obj['name']); //调用offsetExists方法,相当于$obj->offsetExists('name')
//删除该值
unset($obj['name']); //调用offsetUnset方法,相当于$obj->offsetUnset('name')
// 以上4个方法,不一定非要这样写,你可以结合实际写任意逻辑。看起来好像没什么卵用,在laravel等一些框架中大量这种写法,一般用于配置文件比较方便,也可能比较优雅。
我们搞一个简单的配置文件:config.php
<?php
return [
'mysql' => [
//数据库配置
'HOST' =>'127.0.0.1', //数据库地址
'USER' =>'root', //数据库用户名
'PWD' =>'123', //数据库密码
'DATABASEPROT' =>'3306',
'DBNAME' =>'siecomstudy', //数据库名字
'PREFIX' =>'js_', //表前缀
'CHARSET' =>'utf8', //数据库编码
],
'memcache' => [
'CACHE_SERVER' => 'memcached',
'MEMCACHE_HOST' => '127.0.0.1', //memcache链接地址
'MEMCACHE_PORT' => '11211', //memcache端口
'CT'=>true,//数据压缩
],
'mongodb' => [
'MONGODB_HOST' =>'192.168.0.104:27017',
'MONGODB_USER' =>'',
'MONGODB_PWD' =>'',
'MONGODB_CON' =>'',
'MONGODB_DB' =>'last_info'
]
]; 把config.php的配置信息提取出来:
<?php
class Config implements \ArrayAccess {
private $path = ''; //地址
private $confing = [];
public function __construct($path) {
$this->path = $path;
}
public function offsetGet($offset) {
if (empty($this->config[$offset])) {
$this->config[$offset] = require $this->path.".php";
}
return $this->config[$offset];
}
public function offsetExists($offset) {
return isset($this->config[$offset]);
}
public function offsetSet($offset, $value) {
throw new \Exception("不允许外部设置");
}
public function offsetUnset($offset) {
throw new \Exception("不允许外部删除");
}
}
$config = new Config('config/config'); 目录结构config/config.php
$C = $config['config']; //['config']这个是随便命名
print_r($C); //输出全部
echo $C['mysql']['HOST']; //查看mysql的HOST输出:
Array
(
[mysql] => Array
(
[HOST] => 127.0.0.1
[USER] => root
[PWD] => 123
[DATABASEPROT] => 3306
[DBNAME] => siecomstudy
[PREFIX] => js_
[CHARSET] => utf8
)
[memcache] => Array
(
[CACHE_SERVER] => memcached
[MEMCACHE_HOST] => 127.0.0.1
[MEMCACHE_PORT] => 11211
[CT] => 1
)
[mongodb] => Array
(
[MONGODB_HOST] => 192.168.0.104:27017
[MONGODB_USER] =>
[MONGODB_PWD] =>
[MONGODB_CON] =>
[MONGODB_DB] => last_info
)
)
127.0.0.1这样就可以按照数组取了!