#这是框架模式的配置文件
thinkphp/convention.php
二、Array Access
提供像访问数组一样访问对象的能力的接口
offsetExists 检查偏移位置是否存在
offsetGet 获取一个便宜位置的值
offsetSet 设置一个偏移位置的值
offsetUnset 删除一个偏移位置的值
输出
#ya kang te
三 、Yaconf
一个高性能的配置管理
http://www.laruence.com/2015/06/12/3051.html
它在PHP启动的时候, 处理所有的要处理的配置, 然后这些配置就会常驻内存, 随着PHP的生命周期存亡. 避免了每次请求的时候解析配置文件.
安装 https://github.com/laruence/yaconf
找到phpize – 我的是在 /usr/local/Cellar/php/7.3.1/bin/phpize
make && make install
php -i | grep php.ini
需要重启 tp5
输出
四 、load加载深度分析
在什么地方,或者在什么时候,会去进行load形式
thinkphp/library/think/App.php
/**
* 初始化应用或模块
* @access public
* @param string $module 模块名
* @return array
*/
private static function init($module = '')
{
// 定位模块目录
$module = $module ? $module . DS : '';
// 加载初始化文件
if (is_file(APP_PATH . $module . 'init' . EXT)) {
include APP_PATH . $module . 'init' . EXT;
} elseif (is_file(RUNTIME_PATH . $module . 'init' . EXT)) {
include RUNTIME_PATH . $module . 'init' . EXT;
} else {
// 加载模块配置
$config = Config::load(CONF_PATH . $module . 'config' . CONF_EXT);
// 读取数据库配置文件
$filename = CONF_PATH . $module . 'database' . CONF_EXT;
Config::load($filename, 'database');
// 读取扩展配置文件
if (is_dir(CONF_PATH . $module . 'extra')) {
$dir = CONF_PATH . $module . 'extra';
##把这个里面的文件全都放到数组里面去
$files = scandir($dir);
foreach ($files as $file) {
#$file后缀,点的连接 是否等于默认的 CONF_EXT
if ('.' . pathinfo($file, PATHINFO_EXTENSION) === CONF_EXT) {
$filename = $dir . DS . $file;
#pathinfo($file, PATHINFO_FILENAME) 这是文件名的后缀
#这是最终的文件$filename
Config::load($filename, pathinfo($file, PATHINFO_FILENAME));
}
}
}
// 加载应用状态配置
if ($config['app_status']) {
Config::load(CONF_PATH . $module . $config['app_status'] . CONF_EXT);
}
// 加载行为扩展文件
if (is_file(CONF_PATH . $module . 'tags' . EXT)) {
Hook::import(include CONF_PATH . $module . 'tags' . EXT);
}
// 加载公共文件
$path = APP_PATH . $module;
if (is_file($path . 'common' . EXT)) {
include $path . 'common' . EXT;
}
// 加载当前模块语言包
if ($module) {
Lang::load($path . 'lang' . DS . Request::instance()->langset() . EXT);
}
}
return Config::get();
}
<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>
输出:
/www/htdocs/inc
lib.inc.php
php
lib.inc
strpos 如果存在\
#容器
Container::getInstance()
第一个参数是数组
第二个参数是文件名字
parse_ini_file() #第一个参数是路径
$file = "../config/test.ini";
$res = parse_ini_file($file,true)
var_dump($res);
输出
五 、Yaml
yaml 扩展安装
Yaml 基本使用
TP框架代码优化
pecl install yaml
#上层路线的
“…/config/singwa.yaml”
#必须有空格
name: wxh
age: 35
date:
year: 2019
month: 3
day: 11
push:
ss: 111
yaml_parse_file()
thinkphp/library/think/App.php
每次会作用到这个parse,工厂模式会调用这个类里面的parse,直接返回
$this->config
返回这个数组 $this->config;
看最后一个字符是不是点
if('.' == substr($name,-1)){
return $this->pull(substr($name,0,-1));
}