数组式访问-ArrayAccess
以前对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的更多相关文章
- ArrayAccess(数组式访问)
实现该接口后,可以像访问数组一样访问对象. 接口摘要: ArrayAccess { abstract public boolean offsetExists ( mixed $offset ) abs ...
- php使用数组语法访问对象
有一个对象,不过希望能用数组的语法来读写数据,可以使用 实现SPL的ArrayAccess接口来解决. 使用场景:加载配置文件类.larvel框架加载配置文件就这利用数组来操作对象. 数组式访问Obj ...
- 模拟jquery链式访问
一直写代码写代码,博客都快荒废了,眼看一月要过完,不能不留下点记忆,嘿嘿,刚研究了下jquery的链式访问,这么好用的技能我赶紧get了下,研究后略微修改,模拟一个简单的链式访问,下面这段代码支持修改 ...
- C# 类如何声明索引器以提供对类的类似数组的访问的代码
研发期间,将内容过程中比较常用的内容段做个收藏,如下内容内容是关于 C# 类如何声明索引器以提供对类的类似数组的访问.的内容,希望能对各位有用处. using System;using System. ...
- ruby中的链式访问和方法嵌套
先看一道题,这道题是codewars上的一道题,我很早就看到了,但是不会写.等到又看到这道题的时候,我刚看完元编程那本书,觉得是可以搞定它的时候了.废话不多说,先看这道题,题目最开始是为JavaScr ...
- C语言中的数组的访问方式
闲下来,写的代码,很是简单,不解释,代码如下: #include <stdio.h> int main(int argc, char **argv) { char cArray[] = & ...
- 阿里云虚拟主机针对恶意频繁攻击式访问造成CPU爆满的解决方法
最近网站CPU经常爆满,到阿里云提交了工单,工程师给我的处理意见: 您好,虚拟主机CPU占用比较高通常这种情况有两种可能: 一是网站应用程序代码逻辑较复杂,或业务架构效率比较低,在请求了某个网 ...
- (C/C++学习)7.数组及其访问方式
说明:数组的数据类型是一种构造类型,而存储数组的内存是一段连续的存储区域.数组的数据类型决定了连续内存的访问方式,它包括数组的三要素:起始地址.步长以及元素个数. 一.一维数组 1.形式:type 数 ...
- android JNI 一维数组、二维数组的访问与使用
在JNI中访问JAVA类中的整型.浮点型.字符型的数据比较简单,举一个简单的例子,如下: //得到类名 jclass cls = (*env)->GetObjectClass(env, obj) ...
随机推荐
- IIC通讯协议(非原创,转载他人,用于学习)
I2C协议:1.空闲状态 2.开始信号 3.停止信号 4.应答信号 5.数据的有效性 6.数据传输 IIC详解 1.I2C总线具有两根双向信号线,一根是数据线SDA,另一根是时钟线SCL 2.IIC总 ...
- Codeforces828 A. Restaurant Tables
A. Restaurant Tables time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 设计一个BCD码计数器。
BCD码计数器的定义: 对于机器语言,机器与人不同,为了让人更好的了解机器语言的数据输出,选用4位二进制数据表示十进制里的每位数据,这便是BCD码. 以下便是BCD码与十进制对应的码表 0------ ...
- Android框架式编程之MVP架构
MVP(Model-View-Presenter)模式.是将APP的结构分为三层:View - Presenter - Model. View 1. 提供UI交互 2. 在presenter的控制下修 ...
- 浅谈css3长度单位rem,以及移动端布局技巧
rem是什么? rem是css3中新增加的一个单位属性(font size of the root element),根据页面的根节点的字体大小进行转变的单位.root!!!!!!!!!根节点,也就是 ...
- jmeter集合点使用方法:Synchronizing Timer
LR中集合点可以设置多个虚拟用户等待到一个点,同时触发一个事务,以达到模拟真实环境下多个用户同时操作,实现性能测试的最终目的. jmeter中使用Synchronizing Timer实现Lr中集合点 ...
- 顺序栈,链栈,队列java实现
顺序栈 /** * 顺序栈 * */ public class SqStack { //栈的大小 private int maxSize; //栈顶指针 private int top; privat ...
- python之内置装饰器(property/staticmethod/classmethod)
python内置了property.staticmethod.classmethod三个装饰器,有时候我们也会用到,这里简单说明下 1.property 作用:顾名思义把函数装饰成属性 一般我们调用类 ...
- Ubuntu18.04 下修改 root密码
首先打开终端输入命令 sudo passwd root 然后依次是当前用户密码,将要设置root密码,确认root密码.切换root看一下 备注: #符号 是系统用户 root$符号 是你创建的用户 ...
- Excel设置excel打印每页都有表头标题
Excel设置excel打印每页都有表头标题