扩展配置中断应用程序

时间:2016-08-24 08:19:44

标签: php zend-framework2

这是我的代码:

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

不知道这里有什么不对。

2 个答案:

答案 0 :(得分: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

替换它时似乎有效

答案 1 :(得分:0)

您可以在configCache构造函数中执行以下操作,而不是修改zend配置类。当config类将使用数组调用缓存类时,您将控制权传递回带有接收数组的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);
相关问题