一个用于轻松加载和访问配置的 ini 配置加载类。

v1.1.2 2017-12-05 23:33 UTC

This package is auto-updated.

Last update: 2024-09-19 10:17:30 UTC


README

此库提供了创建、读取/加载到内存以及写入 .ini 格式配置文件的能力。

安装

使用 composer 安装

composer require truecastdesign/config

需要 PHP 5.5 或更高版本。

用法

以下是一个基本用法示例

创建配置文件,格式如下

; MySQL database config

config_title = 'mysql'
type = 'mysql'
hostname = 'localhost'
username = 'root'
password  = 'password'
database  = 'dbname'
port = 3306
persistent = true
emulate_prepares = false
compress = true
charset = 'utf8'
buffer = true
<?

# composer autoloader
require '/path/to/vendor/autoload.php';

# load in needed config files into Config object
$TAConfig = new \Truecast\Config('/path/to/config/mysql.ini'); # standard path: __DIR__.'/../app/config/mysql.ini'

# can load multiple config files at once with a comma between. Example: '/path/to/config/mysql.ini, /path/to/config/site.ini'

访问配置值

配置文件中的 "config_title" 是您用来访问该配置文件的密钥词。要访问其他值,您将使用子对象键。

echo $TAConfig->mysql->username; # this would output the string "root" using the above config file.

要使用配置文件中的部分,您可以使用方括号将部分标题包围起来。

示例

config_title="items"

[secion_title]
name="The Name"
version="1.0"
date="2017-02-12"
show=true
sort=2

[secion_title_two]
name="The Name"
version="1.0"
date="2017-02-12"
show=true
sort=2

可以通过在获取值时添加额外的对象级别来访问这些部分值,即使用部分标题。

示例

echo $TAConfig->items->secion_title_two->version;