gpt4 book ai didi

php - Zend 框架 : How to unset a key in Zend_Registry?

转载 作者:可可西里 更新时间:2023-10-31 22:08:40 25 4
gpt4 key购买 nike

我正在测试我的 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;
}

如何取消注册表中的键值?我试过了,但没有用:

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

更新:我真正想知道的是,当未设置配置文件中的“domain”键时,我应该如何测试我的方法是否抛出异常。

最佳答案

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

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

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

您应该考虑到以这种方式编辑注册表不是最佳做法。特别是,Zend_Config 对象一旦被最初实例化就被设计成静态的。

希望我已经充分理解您的问题!

关于php - Zend 框架 : How to unset a key in Zend_Registry?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/615837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com