2013-07-11 35 views
2

我有幾個我想在Zend Framework中使用的常量。我知道我可以在index.php中設置它們,但是在運行PHPUnit測試時不會執行此操作。 (無論如何,在這種情況下)。用Zend Framework定義常量的地方

我還可以在框架中設置這些嗎?問題是常量需要在類之外聲明。 (我不想要類常量)。

如果一切都失敗了,我可以在我的單元測試bootstrap中設置它們,但是如果可能的話我想避免重複。

+0

也http://stackoverflow.com/questions/1805965/php-zend-framework-zend-config-and-global-state?rq=1 – Gordon

回答

5

我總是在bootstrap文件中定義它們。但我確實使用Zend_Registry而不是常量。

Zend_Registry::set('property1', 'value1'); 

//everywhere in your code 
$value = Zend_Registry::get('property1'); 
0

製作自定義庫並在其中定義所有常量。 這對您很有好處,因爲您每次調用控制器時都會調用庫。

您必須在bootstrap和ini文件中定義自定義庫。

這將是一個很好的做法,因爲您可以在某個地方找到每個常量,並且您可以隨時隨地進行更改。

1

值得一提的是,您可以在application.ini文件中定義「真實」常量。 實施例:

//application.ini 
[production] 
constants.ONE = 'HELLO' 
constants.TWO = 'WORLD' 


//Bootstrap.php 
public function setConstants($constants) 
{ 
    // define() is notoriously slow 
    if (function_exists('apc_define_constants')) { 
     apc_define_constants('zf', $constants); 
     return; 
    } 

    foreach ($constants as $name => $value) { 
     if (false === defined($name)) { 
      define($name, $value); 
     } 
    } 
} 
相關問題