Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework: How to unset a key in 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.

like image 584
Andrew Avatar asked Jan 23 '23 19:01

Andrew


1 Answers

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!

like image 84
tombazza Avatar answered Feb 02 '23 09:02

tombazza