Download the PHP package ckr/config without Composer
On this page you can find all versions of the php package ckr/config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package config
Short Description Simple access to multi dimensional arrays, useful for configuration data
License MIT
Informations about the package config
Configuration Library
This package provides a class to manage hierarchical data. A common use case is configuration data.
Note that this package does not provide any readers or writers for config files of different formats. Instead it wraps php arrays and provides easier access to its values.
Why?
I was tired of using too many isset
throghout all my code. The main
advantage of this library is, that you can request
an arbitrary deeply nested value, and provide a default value which
should be returned in case the requested value does not exist:
Actually, if you're using PHP7, this problem is solved:
However, as I prefer to specify the path as root/specific/key
instead
of ['root']['specific']['key']
, I still use this library in my
PHP7 projects.
Usage
You start with a hierarchical data structure, defined as multi dimensional array:
Then you can wrap it in a Config
object:
You can ask for a simple value with get
:
In the example above, "dev" is the default mode. It is returned, if the key 'mode' does not exist in the config data.
You can ask for a value which is nested in a child array by providing a path of keys, separated
by a Slash /
:
If any of the used path parts (keys) don't exist, the default value (null
if
not explicitly specified) is returned.
Say you want to use all of the logging data, e.g. to instantiate your logging instance. For this, you can get a child config object:
To summarize:
Config::get
will always return the value as it was defined originally. No matter, if the value is an array, a class instance, a function or a scalar value.Config::child
expects the given path to point to an (associative) array. It wraps this array in aConfig
and returns that object.
A new value can be set using Config::set
:
This method returns a new instance with a specific part of the config updated (or newly set).
This may be useful if you want to construct a complete configuration object
in multiple steps from different parts of your code,
e.g. to allow different modules to register their configuration or factories.
In the above example, $newConfig
has a value of 'AnotherFactoryClass'
at the path
logging/factory, where the $c
instance is not modified and still has the old value.
(Note that this behaviour is new in Version 2.0, former versions did update the data in place).