上篇文章已经讲解arrayacces的原理,现在来讲解下arrayaccess的实际应用。
一个大型的互联网项目中必然会存在各种配置信息,例如多种数据库信息:mysql,tidb,mongodb,redis,某个业务模块单独的配置信息如比例,额度等等,那么该如何治理配置信息?
PHP项目中大部分的框架都是通过数组来保存配置信息,比如微擎在data.php中囊括了全局所有的配置项。实际上微擎的这种方式是不可取的,随着项目越来越大,配置项越来越多,但是并非每次请求的业务都需要用到配置信息,造成多余的内存占用。理想的配置管理应该是按需加载。
例如在我的框架下存在一个config文件夹,其中包含db.php,rpc.php,文件内容如下:
(1).db.php
<?php return [ 'mysql' => [ 'host' => '127.0.0.1', 'port' => '3306', 'user' => 'root', 'pass' => 'root', 'dbname' => 'dnet' ], 'redis' => [ 'host' => '127.0.0.1', 'port' => '6379' ], ];
(2).rpc.php
<?php return [ 'default' => [ 'ip' => '127.0.0.1', 'port' => 8199, 'pools' => 10, 'local' => true, ], 'order' => [ 'ip' => '127.0.0.1', 'port' => 8199, 'pools' => 10, 'local' => true, ] ];
当然随着项目的扩展,config文件夹会存在一堆这样的配置文件,通过我的配置管理器即可做到按需加载。代码如下:
<?php
/**
* 配置管理器
* Class Configer
*/
class Configer implements ArrayAccess
{
/**
* 配置文件目录
*/
private $path;
/**
* 配置集合
* @var array
*/
private $config = [];
/**
* 配置实例
* @var
*/
private static $instance;
/**
* 初始化配置目录
* Configer constructor.
*/
public function __construct()
{
$this->path = __DIR__ . '/config/';
}
/**
* 配置器单件实例
*/
public static function instance()
{
if (!(self::$instance instanceof Configer))
{
self::$instance = new Configer();
}
return self::$instance;
}
/**
* 检查指定的Key是否存在
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->config[$offset]);
}
/**
* 获取指定的Key值
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset)
{
if (empty($this->config[$offset]))
{
$this->config[$offset] = require $this->path . $offset . '.php';
}
return $this->config[$offset];
}
/**
* 重新设置某个Key的值
* @param mixed $offset
* @param mixed $value
* @throws Exception
*/
public function offsetSet($offset, $value)
{
var_dump($offset);
$this->config[$offset] = $value;
}
/**
* 销毁某个Key的值
* @param mixed $offset
*/
public function offsetUnset($offset)
{
if (isset($this->config[$offset]))
{
$this->config[$offset] = null;
}
}
}
//初始化配置器
$configer = Configer::instance();
//获取DB配置信息
//var_dump($configer['db']['mysql']);
//var_dump($configer['db']['redis']);
//获取RPC配置信息
//var_dump($configer['rpc']['order']);在fpm框架中按需加载的确很重要,但如果是基于swoole的常驻内存性框架,可以直接忽略,遍历文件批量获取即可。
php调用Webservice基本语法如下:$url ='xxxxxxx.cn' //链接服务器端 $client = new SoapClient($url);通过以上语法已经连接到webservice,也可将wsdl在本地使用,...
Redis提供了发布订阅功能,可以用于消息的传输,Redis的发布订阅机制包括三个部分,发布者(publisher),订阅者(subscriber)和频道(channel)。 发布者和订阅者都是Redis客户端,Channel则为Redis服务器端,发布者将消息发送到某个的频道,订阅了这个...
(1).学习目标: 了解常见信号类型(百度PHP支持的信号类型),(2).相关函数学习: (2.1).pcntl_signal函数用于设置一个信号管理器接收进程信号,参数1:信号类型,参数2:回调函数,用于在接收到参数1类型的信...
posix_ttyname - 获取当前终端设备名称。<?php var_dump( posix_ttyname(STDOUT) );我们启动一个终端,执行上面的代码输出:/dev/tty1我们再启动一个终端,执行上面的代码输...
第一次听说端口复用是在mixphp最新版本中发现的,mixphp启动监听9501端口,现在作者说可以多开几个进程来执行mixphp,我心里想了下再启动不是会端口冲突嘛,但是却没有问题,于是下载mixphp的源码解读,原来是启动http服务器使用new Co\Http\Server('0.0....
文章篇幅较长,如果不喜欢看文章的,此处抛出韩顺丰老师的位运算视频,韩老师应该是全网讲php位运算符最详细的一个老师了。链接:https://pan.baidu.com/s/14xj7er8eVSUcJ-jYXyA0GA 提取码:731m 链接:https://pan.baidu.com...