Using class constants in Zend_Config_Ini application.ini

1.3k Views Asked by At

I've got several constants defined in one of my classes and would like to use on of them in my application.ini, is this possible?

logger.main.level = \My\App\Logger::WARNING

This does not seem to work, it just parses it as a string

3

There are 3 best solutions below

0
On BEST ANSWER

If you want to use the value of a constant in your application.ini you need to define the constant in the /public/index.php file (see the APPLICATION_PATH).

If you want to do it cleanly in your code I would eiter do it using the log level manually with the numeric value (assuming the constant is numeric as in Zend_Log) since this value is not likely to change or do it in the index.php and define a new constant named something like LOGLEVEL_WARNING (you can then get the value of the constant statically from your class), which you will be able to use in the application.ini file like logger.main.level = LOGLEVEL_WARNING

My pick would be the numeric value in the application.ini file like this :

resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/../data/logs/application_" DATESTAMP ".log"
resources.log.stream.writerParams.mode = "a"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = 4

Take a look at APPLICATION_PATH and DATESTAMP which are constants that I have defined in my index.php

0
On

I believe that PHP's parse_ini_file() function only has access to constants in the global space and not in the class space.

I haven't been able to use My_Class::MYCONST or My\Class::MYCONST in any .ini file.

0
On

You can define constants in your application.ini file and then call them with the code below in controller.

public function init() {
    //Getting constants from application.ini file
    $registry = Zend_Registry::getInstance();
    $this->_constants = $registry->app_config->constants;
}

where your constants are defined in application.ini file like below

;My Constants
constants.MY_CONSTANT="value"