Search code examples
zend-frameworkzend-formzend-db

what is the benifit of using Zend_Config class?


i am new to zend framework i wana know why we are using this code for connection to database although we can use the below code also which is simple and not including class what is the advantages of including 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);

if i can do like this

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);

Solution

  • in the way you're using Zend_Config it's in fact doesn't help you a lot to have the settings in a config object.

    Usually, in ZF applications, there's a separate application.ini file which contains all the settings:

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

    Then it's convenient to separate environments (production & development for example) into different sections:

    ; 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
    

    meaning loading the config with:

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

    wil return the development config.

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