PHP学习心得


解读配置文件

<pre><code class="language-php">&lt;?php use think\Container; // PHP底层ArrayAccess类巧用 // 实现ArrayAccess(数组式访问)接口 https://www.php.net/manual/zh/class.arrayaccess.php // 手册位置-&gt;语言参考-&gt;预定义接口-&gt;ArrayAccess(数组式访问)接口 // 【引入高性能Yaconf的原因】性能体现 // yaconf.so 鸟哥用C写的一个php底层扩展 https://www.laruence.com/2015/06/12/3051.html // yaconf的文件名,尽量不要和thinkphp配置文件相同,避免冲突 // 手册位置-&gt;函数参考-&gt;其它基本扩展-&gt;Yaconf // 【高性能配置的扩展】 // 快速查找php配置文件位置 php -i | grep php.ini // Config底层类库分析,load加载深度分析 // public/index.php -&gt; // 初始化应用或模块 thinkphp/library/think/App.php init(); // thinkphp/library/think/Config.php // 工厂模式,多个对象有同一个方法,再通过一个新的对象调用多个对象中的相同方法 // 详细请参考 https://blog.csdn.net/gurenshen/article/details/53997373 // 简单工厂模式演示 class dbmysql{ public function conn() { echo '连接上了Mysql'; } } class dbsqlite{ public function conn() { echo '连接上了sqlite'; } } class Factory{ public static function createDb($type = '') { if($type == 'mysql') { return new dbmysql(); }elseif($type == 'sqlite') { return new dbsqlite(); }else{//报错 throw new Exception("Error db type", 1); } } } // yaml扩展 TP框架底层优化 // YAML 是专门用来写配置文件的语言,非常简洁和强大,远比 JSON 格式方便,其主要功能用途类似于XML或JSON。 由于实现简单,解析成本很低,特别适合在脚本语言中使用 // thinkphp框架使用yaml扩展建立.yaml配置文件方法 // 创建test.yaml文件 /config/test.yaml // 创建.env文件 /.env 文件内容为: CONFIG_EXT=".yaml" // 让代码更加优雅,config底层类库优化 // 建立Php.php文件 thinkphp/library/think/config/driver/Php.php namespace think\config\driver; class Php { protected $config; public function __construct($config) { if (is_file($config)) { $config = file_get_contents($config); } $this-&gt;config = $config; } public function parse() { return $this-&gt;config; } } // if ('php' == $type) { 下代码注释 thinkphp/library/think/Config.php // return $this-&gt;set(include $file, $name); // 建立Yaml.php文件 thinkphp/library/think/config/driver/Yaml.php namespace think\config\driver; class Yaml { protected $config; public function __construct($config) { if (function_exists('yaml_parse_file')) { throw new \Exception('不存在yaml扩展'); } if (is_file($config)) { $config = yaml_parse_file($config); } $this-&gt;config = $config; } public function parse() { return $this-&gt;config; } } // elseif ('yaml' == $type &amp;&amp; function_exists('yaml_parse_file')) { 下代码注释 thinkphp/library/think/Config.php // return $this-&gt;set(yaml_parse_file($file), $name); </code></pre>

页面列表

ITEM_HTML