Zend Framework:如何取消Zend_Registry中的密钥?

And*_*rew 4 php registry zend-framework config

我正在测试我的Zend Framework应用程序,并希望测试在注册表中未设置特定键时发生的事情.这是我正在测试的功能:

protected function getDomainFromConfig() {
    $config = Zend_Registry::get('config');
    if (!isset($config->domain)) {
        throw new Exception('Please make sure you have "domain" set in your config file and your config file is being set in the Zend_Registry.');
    }
    return $config->domain;
}
Run Code Online (Sandbox Code Playgroud)

如何取消注册表中的密钥?我试过这个,但它不起作用:

$config = Zend_Registry::get('config');
$config->__unset('domain');
Run Code Online (Sandbox Code Playgroud)

更新:我真正想知道的是,如果未设置配置文件中的"domain"键,我应该如何测试我的方法抛出异常.

tom*_*zza 8

更改配置对象值的唯一真正方法是将其转储到变量,取消设置有问题的项,请求注册表删除配置密钥,然后重置它.

<?php
$registry = Zend_Registry::getInstance();
$config = $registry->get('config');
unset($config->domain);
$registry->offsetUnset('config');
$registry->set('config', $config);
?>
Run Code Online (Sandbox Code Playgroud)

但是,要使其工作,您必须在将Zend_Config对象第一次设置到注册表之前将其设置为可编辑.

您应该考虑以这种方式编辑注册表不是最佳做法.特别是,Zend_Config对象在最初实例化后被设计为静态的.

我希望我能够很好地理解你的问题!