使用Zend_Config类的好处是什么?

Faw*_*oor 1 zend-framework zend-form zend-db

我是zend框架的新手我知道为什么我们使用这个代码连接到数据库虽然我们可以使用下面的代码也很简单但不包括类包含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);
Run Code Online (Sandbox Code Playgroud)

如果我能做到这一点

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);
Run Code Online (Sandbox Code Playgroud)

apo*_*rat 5

在你使用Zend_Config它的方式实际上对配置对象中的设置没有多大帮助.

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

$config = new Zend_Config_Ini('/path/to/config.ini',
                              'production');
Run Code Online (Sandbox Code Playgroud)

然后将环境(例如生产和开发)分成不同的部分很方便:

; 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
Run Code Online (Sandbox Code Playgroud)

意味着加载配置:

$config = new Zend_Config_Ini('/path/to/config.ini',
                              'development');
Run Code Online (Sandbox Code Playgroud)

将返回开发配置.

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