以前对ArrayAccess不是很熟悉,现在整理下下有关ArrayAccess相关的知识,ArrayAccess接口就是提供像访问数组一样访问对象的能力的接口。

接口内容如下:

ArrayAccess {

    //检查一个偏移位置是否存在

    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
namespace lib;
use mpf\core\Di;
class config implements \ArrayAccess{

//定义存储数据的数组
protected $configs;
public function __construct($configs){
  $this->configs = $configs;
  $configs = \lib\model\Home::getWebConfig();
  foreach( $configs as $config ){
    if( !isset($this->configs[$config['sc_key']]) ){
    $this->configs[$config['sc_key']] = $config['sc_content'];
    }
  }
}
public function get($key){
  if( isset($this->configs[$key]) ){
    return $this->configs[$key];
  }elseif( $key == 'caipiao'){
    $this->configs['caipiao'] = \lib\model\Home::getLcs();
    return $this->configs[$key];
  }elseif( $key == 'user_money' ){
    if( isset($_SESSION['uid']) ){
  if( $_SESSION['utype'] == 5 ){
    $sql = 'select money from inner_user where uid=?';
  }else{
    $sql = 'select money from user where uid=?';
  }
    $this->configs['user_money'] = \mpf\core\Di::$Di->db->prepare_query($sql,[getUid()])->fetch(\PDO::FETCH_COLUMN);
    return $this->configs['user_money'];
  }
}
}
public function offsetExists($index){
  return isset($this->configs[$index]);
}
public function offsetGet($index){
  return $this->configs[$index];
}
public function offsetSet($index,$val){
  $this->configs[$index] = $val;
}
public function offsetUnset($index){
  unset($this->configs[$index]);
}
}

这样可以使用config对象来直接访问配置信息内容。

---------------------------------

配置程序:

我们可以通过ArrayAccess利用配置文件来控制程序。

1. 在项目更目录下创建一个config目录 
2. 在config目录下创建相应的配置文件,比如app.php 和 database.php。文件程序如下

app.php

<?php

return [
'name' => 'app name',
'version' => 'v1.0.0'
];

database.php

<?php

return [
'mysql' => [
'host' => 'localhost',
'user' => 'root',
'password' => '12345678'
]
];

3. Config.php实现ArrayAccess

<?php

namespace Config;

class Config implements \ArrayAccess
{
private $config = []; private static $instance; private $path; private function __construct()
{
$this->path = __DIR__."/config/";
} public static function instance()
{
if (!(self::$instance instanceof Config)) {
self::$instance = new Config();
}
return self::$instance;
} public function offsetExists($offset)
{
return isset($this->config[$offset]);
} public function offsetGet($offset)
{
if (empty($this->config[$offset])) {
$this->config[$offset] = require $this->path.$offset.".php";
}
return $this->config[$offset];
} public function offsetSet($offset, $value)
{
throw new \Exception('不提供设置配置');
} public function offsetUnset($offset)
{
throw new \Exception('不提供删除配置');
}
} $config = Config::instance(); //获取app.php 文件的 name
echo $config['app']['name'].PHP_EOL; //app name //获取database.php文件mysql的user配置
echo $config['database']['mysql']['user'].PHP_EOL; // root

数组式访问-ArrayAccess的更多相关文章

  1. ArrayAccess(数组式访问)

    实现该接口后,可以像访问数组一样访问对象. 接口摘要: ArrayAccess { abstract public boolean offsetExists ( mixed $offset ) abs ...

  2. php使用数组语法访问对象

    有一个对象,不过希望能用数组的语法来读写数据,可以使用 实现SPL的ArrayAccess接口来解决. 使用场景:加载配置文件类.larvel框架加载配置文件就这利用数组来操作对象. 数组式访问Obj ...

  3. 模拟jquery链式访问

    一直写代码写代码,博客都快荒废了,眼看一月要过完,不能不留下点记忆,嘿嘿,刚研究了下jquery的链式访问,这么好用的技能我赶紧get了下,研究后略微修改,模拟一个简单的链式访问,下面这段代码支持修改 ...

  4. C# 类如何声明索引器以提供对类的类似数组的访问的代码

    研发期间,将内容过程中比较常用的内容段做个收藏,如下内容内容是关于 C# 类如何声明索引器以提供对类的类似数组的访问.的内容,希望能对各位有用处. using System;using System. ...

  5. ruby中的链式访问和方法嵌套

    先看一道题,这道题是codewars上的一道题,我很早就看到了,但是不会写.等到又看到这道题的时候,我刚看完元编程那本书,觉得是可以搞定它的时候了.废话不多说,先看这道题,题目最开始是为JavaScr ...

  6. C语言中的数组的访问方式

    闲下来,写的代码,很是简单,不解释,代码如下: #include <stdio.h> int main(int argc, char **argv) { char cArray[] = & ...

  7. 阿里云虚拟主机针对恶意频繁攻击式访问造成CPU爆满的解决方法

    最近网站CPU经常爆满,到阿里云提交了工单,工程师给我的处理意见:   您好,虚拟主机CPU占用比较高通常这种情况有两种可能:   一是网站应用程序代码逻辑较复杂,或业务架构效率比较低,在请求了某个网 ...

  8. (C/C++学习)7.数组及其访问方式

    说明:数组的数据类型是一种构造类型,而存储数组的内存是一段连续的存储区域.数组的数据类型决定了连续内存的访问方式,它包括数组的三要素:起始地址.步长以及元素个数. 一.一维数组 1.形式:type 数 ...

  9. android JNI 一维数组、二维数组的访问与使用

    在JNI中访问JAVA类中的整型.浮点型.字符型的数据比较简单,举一个简单的例子,如下: //得到类名 jclass cls = (*env)->GetObjectClass(env, obj) ...

随机推荐

  1. 读取嵌入到word的Excel对象

    Word.Document doc = this._wordApplication.Documents.Add(@"C:\Users\linmeicheng\Desktop\新建文件夹 (3 ...

  2. PHP直接查看换取的图片

    <!doctype html><head> <title></title> <meta http-equiv="Content-Type ...

  3. 18个分形图形的GIF动画演示

    这里提供18个几何线段分形的GIF动画图像.图形颜色是白色,背景色为黑色,使用最基本的黑与白以表现分形图形. (1)科赫(Koch)雪花   (2)列维(levy)曲线   (3)龙形曲线(Drago ...

  4. 去除swagger ui的红色 error 错误提示

    去除swagger ui的红色 error 错误提示 自定义js文件中加入以下的代码. 加入自定义的js方法看这里 http://www.cnblogs.com/wang2650/archive/20 ...

  5. 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件

    [源码下载] 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件 作者:webabcd 介绍 ...

  6. CVE-2015-1641 Office类型混淆漏洞及shellcode分析

    作者:枕边月亮 原文来自:CVE-2015-1641 Office类型混淆漏洞及shellcode分析 0x1实验环境:Win7_32位,Office2007 0x2工具:Windbg,OD,火绒剑, ...

  7. JS应用实例2:轮播图

    在学习轮播图之前,要先会切换图片: 找三张图片,命名1.jpg,2.jpg,3.jpg 示例: <!DOCTYPE html> <html> <head> < ...

  8. 第六篇: 分布式配置中心(Spring Cloud Config)

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件. 在Spring Cloud中,有分布式配置中心组件spring cloud confi ...

  9. vue.js多页面开发环境搭建

    利用 vue-cli 搭建的项目大都是单页面应用项目,对于简单的项目,单页面就能满足要求.但对于有多个子项目的应用,如果创建多个单页面,显示有点重复,特别是 node_modules 会有多份相同的. ...

  10. 关于springboot aop 俩次调用的问题 aop多次调用

    由于我在springboot 启动类中 给我的切面类进行了赋值  即@Bean 然而我在切面类中加了@Component 导致 springboot 注入了俩个 bean  所以导致 aop 多次执行 ...