PHP的获取配置文件,支持多维数组,没有递归

文章介绍了一种在PHP中通过魔术方法`__callStatic`和`__get`实现动态访问配置文件内容的方法。配置信息存储在`config.php`中,通过`Config`类的静态调用可以便捷地获取和操作配置,如`Config::redis()->version`。这种设计允许方便的链式调用和获取数组结构的配置值。
摘要由CSDN通过智能技术生成

配置文件内容


<?php
$GLOBALS['config']['redis'] = [
    'version' => '1',
    'conn' => [
        'localhost' => ['127.0.0.1', 6379, '']
    ],
];

用法


$rs = Config::redis()->version; //1
$rs = Config::redis()->conn->data(); //conn的值
$rs = Config::redis()->conn->localhost->data(); //conn['localhost']的值
$rs = Config::redis()->conn->keys(); //获取conn的下所有键名, 等同于array_keys()方法,其他还有 values() column()

原理解释

  1. 利用PHP的 `__callStatic($name, $args)` 魔术方法, 所有找不到的静态调用都会被这个方法调用

  1. 这个方法返回Config.php的实例化对象

  1. Config.php实例化的时候,会加载redis.php, 把redis的配置读入到 `Config.php::$data` 中

  1. 利用PHP的 `__get($key)` 魔术方法, 如果获取的成员变量找不到, 就会尝试从`$data` 中读取

  1. 如果`$data`中存在`$key`, 且是`string`或`int`或`float`等标量的时候就会直接返回

  1. 如果`$data`中存在`$key` 但是`array`等标量时, 就返回`$this` 方便后续链式调用

  1. 如果想要返回数组的话, 可以调用`data()`方法

代码参考


<?php
namespace Libs;

class Config
{
    public $raw;
    public $data;

    //读取配置文件, 初始化成员变量
    public function __construct($key='', $args=[])
    {
        if (!empty($key)) {
            $cache_file = 'config_cache_'.ENV; 
            $cache_path = CONFIGPATH.$cache_file.'.php'; //当前运行环境的所有配置的缓存文件路径
            $config_path = CONFIGPATH.$key.'.php'; //某个配置文件路径
            
            if (file_exists($cache_path)) {
                if (isset($GLOBALS[$cache_file][$key])) {
                    $this->raw = $this->data = $GLOBALS[$cache_file][$key];
                } else {
                    include($cache_path);
                    $this->raw = $this->data = $GLOBALS[$cache_file][$key] ?? null;
                }

            } elseif (file_exists($config_path)) {
                if (isset($GLOBALS['config'][$key])) {
                    $this->raw = $this->data = $GLOBALS['config'][$key];
                } else {
                    include($config_path);
                    $this->raw = $this->data = $GLOBALS['config'][$key] ?? null;
                } 
                
            } else {
                $this->raw = $this->data = null;
            }
        }
    }

    //直接给成员变量赋值, 不用读取配置变量
    public static function setData($data)
    {
        $obj = new self();
        $obj->raw = $obj->data = $data;
        return $obj;
    }

    //用来返回实例化对象
    public static function __callStatic($name, $args)
    {
        return new self($name, $args);
    }

    //获取不存在的成员变量, 并更新成员变量$data
    public function __get($key)
    {
        if (isset($this->data[$key])) {
            $tmp = $this->data[$key];
            if (is_scalar($tmp) || is_null($tmp)) {
                return $tmp;
            } else {
                $this->data = $tmp; //覆盖当前值
                return $this; //便于链式调用
            }
        } else {
            return null;
        }
    }

    public function __toString()
    {
        return json_encode($this->data);
    }

    //获取配置文件所有变量, 也防止变量中有键名为`raw`的数据
    public function raw()
    {
        return $this->raw;
    }

    //获取当前值
    public function data()
    {
        return $this->data;
    }

    //获取所有键名
    public function keys()
    {
        return array_keys($this->data);
    }

    //获取所有值
    public function values()
    {
        return array_values($this->data);
    }

    //取出某一列的值
    public function column($field, $index=null)
    {
        return array_column($this->data, $field, $index);
    }
}

码云: myDcool/MengPHP

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值