2016-08-24 46 views
0

這裏是我的代碼:擴展配置打破應用

// Yoda namespace 
namespace Yoda\Application\Config\Feature; 

// use zend config 
use Zend\Config\Config; 

// CacheConfig class 
class CacheConfig extends Config 
{ 
    /** 
    * Default cache type for now 
    * 
    * @var string 
    */ 
    const DEFAULT_CACHE_TYPE = 'filesystem'; 

    /** 
    * Default cache ttl for now 
    * 
    * @var integer 
    */ 
    const DEFAULT_CACHE_TTL = 3600; 

    /** 
    * Constructor. Creates config data for caching 
    */ 
    public function __construct() 
    {    
     $config=[ 
      'name'=> static::DEFAULT_CACHE_TYPE, 
      'options' => [ 
       'ttl' => static::DEFAULT_CACHE_TTL, 
       'cache_dir' => '/var/www/html/yoda/data/cache' 
      ] 
     ]; 
     parent::__construct($config,true); 
    } 
} 

當我使用此代碼的應用程序中斷,並說The localhost page isn't working但是當我剛剛在配置數組傳遞到一個標準的Zend Config對象,它工作正常。

這裏是我使用碼:

  $config=[ 
       'name'=> 'filesystem', 
       'options' => [ 
        'ttl' => 3600, 
        'cache_dir' => '/var/www/html/yoda/data/cache' 
       ] 
      ];    
      //works fine 
      $configCache = new Config($config); 

      //breaks     
      $configCache = new CacheConfig(); 

不知道什麼是錯在這裏。

+0

'static ::'或'self ::'?我認爲引用靜態屬性應該是'self ::' – Kamran

+0

nope沒有解決 – jkushner

回答

0

這是因爲在Config類中構造函數會加載它自己的靜態實例。當我這樣做:

public function __construct() 
{    
    $config=[ 
     'name'=> static::DEFAULT_CACHE_TYPE, 
     'options' => [ 
      'ttl' => static::DEFAULT_CACHE_TTL, 
      'cache_dir' => yoda::registry('cache_dir') 
     ] 
    ]; 
    $this->allowModifications = true;  
    foreach ($config as $key => $value) { 
     if (is_array($value)) { 
      $this->data[$key] = new Config($value, $this->allowModifications); 
     } else { 
      $this->data[$key] = $value; 
     } 
    } 
} 

看來,當我和Config

0

不是修改Zend的配置類,你可以按照你的configCache構造做的更換工作。當config類將數組調用到緩存類時,您可以將控制權交還給接收數組的配置類。然後它會正確設置配置對象。錯誤是因爲Static Bindings

/** 
    * Constructor. Creates config data for caching 
    */ 
    public function __construct($arr = []) 
     {  
      $config=[ 
       'name'=> static::DEFAULT_CACHE_TYPE, 
       'options' => [ 
        'ttl' => static::DEFAULT_CACHE_TTL, 
        'cache_dir' => '/var/www/html/yoda/data/cache' 
       ] 
      ]; 
      if (count($arr) > 0) 
      { 
       parent::__construct($arr,true); 
      } 
      else 
      { 
       parent::__construct($config,true); 
      } 
     } 

    $configCache = new CacheConfig(); 
    print_r($configCache);