doumen5895 2009-03-05 17:51
浏览 17
已采纳

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

I am testing my Zend Framework application and would like to test that something happens when a particular key is not set in the registry. This is the function that I am testing:

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;
}

How can I unset a key in the registry? I tried this, but it did not work:

$config = Zend_Registry::get('config');
$config->__unset('domain');

Update: What I really want to know is how should I test that my method throws an exception when the "domain" key in the config file is not set.

  • 写回答

5条回答 默认 最新

  • drrhr20884 2009-03-06 12:56
    关注

    The only real way to change the value of the config object would be to dump it to a variable, unset the item in question, ask the registry to delete the key for config, and then reset it.

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

    However, for this to work you would have to have set the Zend_Config object to editable before it is set into the registry the first time.

    You should consider that it is not best practice to edit the registry in this way. In particular, the Zend_Config object is designed to be static once it is instantiated originally.

    I hope I have understood your problem well enough!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启