The Yaf_Config_Ini class

(Yaf >=1.0.0)

Introduction

Yaf_Config_Ini enables developers to store configuration data in a familiar INI format and read it in the application by using nested object property syntax. The INI format is specialized to provide both the ability to have a hierarchy of configuration data keys and inheritance between configuration data sections. Configuration data hierarchies are supported by separating the keys with the dot or period character ("."). A section may extend or inherit from another section by following the section name with a colon character (":") and the name of the section from which data are to be inherited.

Note:

Yaf_Config_Ini utilizes the parse_ini_file() PHP function. Please review this documentation to be aware of its specific behaviors, which propagate to Yaf_Config_Ini, such as how the special values of "true", "false", "yes", "no", and "null" are handled.

Tip

Yaf_Config_Ini parses the configuration file on every request. If an application manages a large amount of (mostly static) configuration, consider the Yaconf extension instead: it keeps configuration in shared memory across the whole PHP lifecycle, so it is parsed only once and can be accessed much more efficiently. Yaconf understands the very same INI syntax — keys separated by dots, and inheritance between sections through "[section : parent]" — so the existing application.ini can be served by Yaconf as-is; only the way it is passed to Yaf_Application::__construct() changes (see the tip in Application Configuration).

Class synopsis

class Yaf_Config_Ini extends Yaf_Config_Abstract implements Iterator, ArrayAccess, Countable {
/* Properties */
/* Methods */
publicfunction __construct(mixed $config_file, string $section = NULL)
public function count(): int
public function current(): mixed
public function get(string $name = NULL): mixed
public function __isset(string $name): bool
public function key(): mixed
public function next(): void
public function offsetExists(mixed $name): bool
public function offsetGet(mixed $name): mixed
public function offsetSet(mixed $name, mixed $value): void
public function offsetUnset(mixed $name): void
public function readonly(): bool
public function rewind(): void
public function set(string $name, mixed $value): bool
public function toArray(): array
public function valid(): bool
/* Inherited methods */
public function Yaf_Config_Abstract::get(string $name = NULL): mixed
abstract public function Yaf_Config_Abstract::readonly(): bool
abstract public function Yaf_Config_Abstract::set(string $name, mixed $value): bool
}

Properties

_config

_readonly

Examples

Example #1 Yaf_Config_Ini()example

This example illustrates a basic use of Yaf_Config_Ini for loading configuration data from an INI file. In this example there are configuration data for both a production system and for a staging system. Because the staging system configuration data are very similar to those for production, the staging section inherits from the production section. In this case, the decision is arbitrary and could have been written conversely, with the production section inheriting from the staging section, though this may not be the case for more complex situations. Suppose, then, that the following configuration data are contained in /path/to/config.ini:

; Production site configuration data
[production]
webhost                  = www.example.com
database.adapter         = pdo_mysql
database.params.host     = db.example.com
database.params.username = dbuser
database.params.password = secret
database.params.dbname   = dbname
 
; Staging site configuration data inherits from production and
; overrides values as necessary
[staging : production]
database.params.host     = dev.example.com
database.params.username = devuser
database.params.password = devsecret
<?php
$config = new Yaf_Config_Ini('/path/to/config.ini', 'staging');
 
var_dump($config->database->params->host); 
var_dump($config->database->params->dbname);
var_dump($config->get("database.params.username"));
?>

The above example will output something similar to:

string(15) "dev.example.com"
string(6) "dbname"
string(7) "devuser"

Table of Contents

add a note

User Contributed Notes 3 notes

up
2
lee dot howarth dot 90 at gmail dot com
12 years ago
@flowithwind 

var_dump($config -> toArray()[ 'type' ][ 18 ][ 'text' ]);

string 'abc' (length=3)
up
1
zzxiaoman at gmail dot com
11 years ago
when i use Yaf_Config_ini with these lines:

type.18.text=abc
type.8.text=ddf
type.0.text=fjdsklf

You can through this way

$$configArr = $config->toArray();
var_dump($configArr['type'][18]['text']);

result:
abc
up
1
Mark
11 years ago
/conf/db.ini

[product]
database.params.host           = localhost
database.params.port           = 5432 
database.params.dbname    = postgres
database.params.username = 'postgres'
database.params.password  = 123456

<?php
       $config  = new Yaf_Config_ini('../conf/db.ini','product');
        $config = $config->toArray();
        $host       =   $config['database']['params']['host'];
        $port       =   $config['database']['params']['port'];
        $database   =   $config['database']['params']['dbname'];
        $username        =   $config['database']['params'['username'];        
        $password   =   $config['database']['params']['password']; 
        $pg_conn = pg_connect("host='$host' port='$port' dbname='$database'  user='$username' password='$password' ");
?>