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 them 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.
Class synopsis
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
- Yaf_Config_Ini::__construct — Yaf_Config_Ini constructor
- Yaf_Config_Ini::count — The count purpose
- Yaf_Config_Ini::current — The current purpose
- Yaf_Config_Ini::__get — The __get purpose
- Yaf_Config_Ini::__isset — The __isset purpose
- Yaf_Config_Ini::key — The key purpose
- Yaf_Config_Ini::next — The next purpose
- Yaf_Config_Ini::offsetExists — The offsetExists purpose
- Yaf_Config_Ini::offsetGet — The offsetGet purpose
- Yaf_Config_Ini::offsetSet — The offsetSet purpose
- Yaf_Config_Ini::offsetUnset — The offsetUnset purpose
- Yaf_Config_Ini::readonly — The readonly purpose
- Yaf_Config_Ini::rewind — The rewind purpose
- Yaf_Config_Ini::__set — The __set purpose
- Yaf_Config_Ini::toArray — Returns a PHP array
- Yaf_Config_Ini::valid — The valid purpose
- Введение
- Установка и настройка
- Предопределенные константы
- Примеры
- Application Configuration
- The Yaf_Application class
- The Yaf_Bootstrap_Abstract class
- The Yaf_Dispatcher class
- The Yaf_Config_Abstract class
- The Yaf_Config_Ini class
- The Yaf_Config_Simple class
- The Yaf_Controller_Abstract class
- The Yaf_Action_Abstract class
- The Yaf_View_Interface class
- The Yaf_View_Simple class
- The Yaf_Loader class
- The Yaf_Plugin_Abstract class
- The Yaf_Registry class
- The Yaf_Request_Abstract class
- The Yaf_Request_Http class
- The Yaf_Request_Simple class
- The Yaf_Response_Abstract class
- The Yaf_Route_Interface class
- The Yaf_Route_Map class
- The Yaf_Route_Regex class
- The Yaf_Route_Rewrite class
- The Yaf_Router class
- The Yaf_Route_Simple class
- The Yaf_Route_Static class
- The Yaf_Route_Supervar class
- The Yaf_Session class
- The Yaf_Exception class
- The Yaf_Exception_TypeError class
- The Yaf_Exception_StartupError class
- The Yaf_Exception_DispatchFailed class
- The Yaf_Exception_RouterFailed class
- The Yaf_Exception_LoadFailed class
- The Yaf_Exception_LoadFailed_Module class
- The Yaf_Exception_LoadFailed_Controller class
- The Yaf_Exception_LoadFailed_Action class
- The Yaf_Exception_LoadFailed_View class
Коментарии
@flowithwind
var_dump($config -> toArray()[ 'type' ][ 18 ][ 'text' ]);
string 'abc' (length=3)
/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' ");
?>
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