ZF-6381: Second param to Zend_Registry::get() for default value

Issue Type: Improvement Created: 2009-04-22T08:35:39.000+0000 Last Updated: 2012-11-20T21:37:43.000+0000 Status: Open Fix version(s): Reporter: Eric Freed (efreed) Assignee: None Tags: - Zend_Registry

Related issues: Attachments:


Add an optional second parameter to Zend_Registry::get()

In the case of if (!$instance->offsetExists($index)) instead of always throwing an exception, look to see if the second param is not null and return that value. If it's null, then go ahead and throw the exception.

This greatly improves the usability of this method as I won't have to always put it in a try/catch block when there is an acceptable default.


Posted by Stefan Gehrig (sgehrig) on 2009-11-19T00:55:35.000+0000

I see a small problem here: How would you differentiate between "my default value is NULL" and "please throw an exception if key is not found"? Most likely that would require a third argument to Zend_Registry::get() - such as $throwOnNotFound or something like that.

Posted by Eric Freed (efreed) on 2009-11-19T11:16:48.000+0000

Option 1) (the assumed option requested by this issue) The user just doesn't get to use NULL, that's the price you pay for wanting a throw exception feature.
This is very easily worked around by passing a blank string or a 0 and using that response.

Option 2) Don't have the default be NULL, have it be something unusual like 'throw_exception' Then you can compare to that fixed string and know if an exception should be thrown.

Option 3) Don't define a second parameter and use func_get_arg(1) This president has already been set with the isValid() method in Zend_Validate_Abstract/Zend_Validate_Interface but is difficult to document.

Posted by Thomas Weidner (thomas) on 2010-03-21T10:26:00.000+0000

Reassigned to component maintainer

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.