sigjlr/phpconfig

加载 INI 配置文件

0.1.0 2013-09-25 13:34 UTC

This package is not auto-updated.

Last update: 2024-09-23 16:26:46 UTC


README

一个简单的 .ini 配置文件读取器。

## 安装 您可以使用 Composer(推荐)或手动安装 phpConfig。

## 使用 此示例假设您正在使用 Composer 或其他符合 PSR-0 标准的自动加载器自动加载依赖项。

//Create a new PhpConfig
$config = new \PhpConfig\PhpConfig(); 

//Add any ini resources
$config->addResource('config.ini');
//You can add many resource in a single line
$config->addResource('config.ini', 'config2.ini' );

//Load the resources and produce the configuration array
//If the same key is present in many resources, the last one will be preserved.
$config->load();

//Get the configuration array
$myConfiguration = $config->getConfig();

要拒绝浏览器访问 .ini 文件,请在 .htaccess 中添加此指令

<Files *.ini> 
    Order deny,allow
    Deny from all
</Files>

例如,考虑以下 ini 文件

Global.ini

[Section_A]
param1 = GlobalA1
param2 = GlobalA2

Local.ini

[Section_A]
param1 = LocalA1
param3 = LocalA3

[Section_B]
param1 = LocalB1
param2 = LocalB2

使用此代码

//Create a new PhpConfig
$config = new \PhpConfig\PhpConfig(); 

//Add resources
$config->addResource('Global.ini', 'Local.ini' );

$config->load();
$myConfiguration = $config->getConfig();

print_r($myConfiguration);
/*
you get this array:

Array(
  [Section_A]=>array(
    [param1] => LocalA1,
    [param2]=> globalA2,
    [param3]=> LocalA3
   ),
   [Section_B]=>array(
    [param1] => LocalB1,
    [param2]=> LocalA2
   )
)
*/

## 许可证 phpConfig 在 MIT 公共许可证下发布