Flame war starting :)
Let's just put it this way, the Zend_Registry is simply a proper OO
design pattern implementation similar to php $GLOBALS. If you don't
care about design patterns then you'll probably find Zend_Registry
pointless but if you do care about design patterns then you'll use it.
if (!Zend_Registry::isRegistered('somevar')) {
//do some processing
Zend_Registry::set('somevar', $value);
} else {
$value = Zend_Registry::get('somevar');
}
is pretty much the same as
if (!array_key_exists( 'somevar', $GLOBALS )) {
//do some processing
$GLOBALS['somevar'] = $value;
} else {
$value = $GLOBALS['somevar'];
}
Personally, I prefer registry methods as it's easier to understand
when quickly reading code for inexperienced developers.
Avoiding $GLOBALS also eliminates the possibility of accidentally
overriding the value. $GLOBALS['var'] = 'foo' is exactly the same as
$var = 'foo' if you're in the global scope so just searching for
$GLOBALS isn't valid as any $var in global scope is also included.
It's all just personal preference, there's probably no technical
advantage though I haven't done any tests on that.
- Jeff
On 24-Apr-08, at 8:03 AM, Sancar Saran wrote:
On Thursday 24 April 2008 14:39:51 Matthew Weier O'Phinney wrote:
-- Greg Donald <[EMAIL PROTECTED]> wrote
(on Wednesday, 23 April 2008, 04:04 PM -0500):
What is gained by using Zend_Registry? Seems to me I could more
easily use the existing PHP $GLOBALS or even $GLOBALS['registry']
if I
wanted/needed a namespace.
Zend_Registry::set('index', $value);
versus
$GLOBALS['index'] = $value;
Globals are a really bad idea in almost every case, and they lead to
some really hairy problems with scoping, determining how and when a
variable is set, and much more. Do some googling for 'php globals
problems', and you'll start to get the idea. Try wrapping existing
code
that relies on globals in an MVC framework some time, and you'll
start
to see the issues quickly and noisily.
A Registry is a design pattern that is used to solve the problems of
globals in a more maintainable and predictable way.
That said, you're free to use $GLOBALS instead of Zend_Registry if
you
still fail to see the point or simply prefer $GLOBALS.
Hello There,
I strongly against your arguments.
Using globals with namespace does not create any problems. Searcing
google
"php globals problems" produces nothing.
I strongly believe "using globals in php creates problems" is a
MYTH. This
myth creted by programmers who has strong c and c++ backgrounds.
Large and popular projects like phpAdodb and Typo3 (as far as I
know) uses
GLOBALS effecively and problem free.
I use GLOBALS for
Language key pairs
Static Config variables
(Db, memcache, etc) resource pointers
I was much faster and pratic than any oo way..
Regards
Sancar