Accessing constants in config/main file

Hi all,

 I want to access a veriable from constant component file in the config/main.php file. For example,      


 I have a ConfigManager Class  in my component dir as  




<?php

class ConfigManager {

        public static $dbUserName = 'root';

        public static $dbPassword = 'test123';

}

?>




And in config file I want to do it like this -




'db'=>array(

        'class'=>'CDbConnection',

        'connectionString'=>'mysql:host=localhost;dbname=dbname',

        'username'=> ConfigManager::$dbUserName,

        'password'=> ConfigManager::$dbPassword,

        'emulatePrepare'=>true,

),




But it is giving require error. Can’t find class ConfigManager. Is it possible to do it like this?

Thanks!

The components aren’t loaded until after the config file. Not surprising, given that the config file contains the information needed to load them. You could include/require ConfigManager.php in your config file.

Let me ask one question: What do you win by using such a ConfigManager class?

Hi Mike,

Actually I have some other configuration parameters like




     $serverTimeZone = 'Asia/Calcutta';




  What I would like to have single configuration file that just need to be edited while configuring the application. So I want to have ConfigManager file to place all the configurations and it will configure rest of the application. 





    May be its not right idea&#33;

Please replay!

Thanks again.

There are lot of ways to do something similar. My favorite approach is to keep a file local.php in config/ that gets merged into the main.php config array. That way you can override/add settings in your local config file. Similar to this:

http://www.yiiframework.com/doc/cookbook/32/