nick-jones/simpleconfig

一个基本的配置容器类

v0.1 2014-02-06 14:10 UTC

This package is not auto-updated.

Last update: 2024-09-24 07:17:04 UTC


README

SimpleConfig 是一个基本的配置容器,用于PHP。

安装

为了下载依赖项并检查版本兼容性,您需要在项目根目录下运行 composer

用法

可以在构造函数中提供值和工厂

$container = new \SimpleConfig\Container(
    [
        'field1' => 'value',
        // etc
    ],
    [
        'field2' => function() {
            return new Foo();
        }
    ]
);

或者,可以使用 offsetSetfactory 分别添加值和工厂。

Container 类实现了 ArrayAccess 接口,因此可以使用数组语法直接访问值,例如。

echo $container['field1']; // prints "value"

工厂在字段检索时只调用一次,结果值将被缓存以供将来使用。工厂可以通过简单地使用 $this 从容器中检索其他值,例如。

$container->factory('foo', function() {
    $class = $this['foo.class'];
    return new $class();
});