2012-01-11 87 views
1

我是新来的Zend Framework我瓦纳知道为什么我们使用此代码连接到数据库,虽然我们可以使用下面的代码也具有简单,不包括类是什么,包括Zend_Config的()使用Zend_Config类的好处是什么?

的优势
require_once 'Zend/Config.php'; 
$arrConfig = array(
    'webhost'=>'localhost', 
    'appName'=>'My First Zend', 
    'database'=>array(
     'dbhost'=>'localhost', 
     'dbname'=>'zend', 
     'dbuser'=>'root', 
     'dbpass'=>'admin' 
    ) 
); 

$config = new Zend_Config($arrConfig); 
$params = array('host' =>$config->database->dbhost, 
      'username' =>$config->database->dbuser, 
      'password' =>$config->database->dbpass, 
      'dbname' =>$config->database->dbname 
      ); 
$DB = new Zend_Db_Adapter_Pdo_Mysql($params); 
$DB->setFetchMode(Zend_Db::FETCH_OBJ); 

如果我能像这样

include_once 'Zend/Db/Adapter/Pdo/Mysql.php'; 
$params = array('host' => 'localhost', 
     'username' => 'root', 
     'password' => '', 
     'dbname'  => 'zend' 
      ); 
$DB = new Zend_Db_Adapter_Pdo_Mysql($params); 
$DB->setFetchMode(Zend_Db::FETCH_OBJ); 

回答

5
的方式

你使用Zend_Config它其实并不能帮助你很多在配置对象的设置做。

通常,在ZF的应用,还有其中包含的所有设置一个单独的application.ini文件:

$config = new Zend_Config_Ini('/path/to/config.ini', 
           'production'); 

然后,它的方便,独立的环境(生产&发展)成不同的部分:

; Production site configuration data 
[production] 
database.adapter   = pdo_mysql 
database.params.host  = db.example.com 
database.params.username = dbuser 
database.params.password = secret 
database.params.dbname = dbname 

; Development site configuration data inherits from production and 
; overrides values as necessary 
[development : production] 
database.params.host  = dev.example.com 
database.params.username = devuser 
database.params.password = devsecret 

意味着与装载配置:

$config = new Zend_Config_Ini('/path/to/config.ini', 
           'development'); 

将返回开发配置。

http://framework.zend.com/manual/en/zend.config.adapters.ini.html