简介
Example #1 使用 Zend_Config 本身
通常用户可以使用象 // 给出一个配置数据的数组 $configArray = array( 'webhost' => 'www.example.com', 'database' => array( 'adapter' => 'pdo_mysql', 'params' => array( 'host' => 'db.example.com', 'username' => 'dbuser', 'password' => 'secret', 'dbname' => 'mydatabase' ) ) ); // 基于配置数据创建面向对象的 wrapper $config = new Zend_Config($configArray); // 输出配置数据 (结果在'www.example.com'中) echo $config->webhost; // 使用配置数据来连接数据库 $db = Zend_Db::factory($config->database->adapter, $config->database->params->toArray()); // 另外的用法:简单地传递 Zend_Config 对象。 // Zend_Db factory 知道如何翻译它。 $db = Zend_Db::factory($config->database);
如上例所示,
连同面向对象访问数据值, $host = $config->database->get('host', 'localhost'); Example #2 Using Zend_Config with a PHP Configuration File It is often desirable to use a pure PHP-based configuration file. The following code illustrates how easily this can be accomplished: // config.php return array( 'webhost' => 'www.example.com', 'database' => array( 'adapter' => 'pdo_mysql', 'params' => array( 'host' => 'db.example.com', 'username' => 'dbuser', 'password' => 'secret', 'dbname' => 'mydatabase' ) ) ); // Configuration consumption $config = new Zend_Config(require 'config.php'); // Print a configuration datum (results in 'www.example.com') echo $config->webhost;
|