View Single Post
  #5 (permalink)  
Old 04-17-2008, 02:41 PM
Elemental's Avatar
Elemental Elemental is offline
Senior Member
 
Join Date: Jul 2007
Posts: 122
Default #zftalk chat on this topic

Quote:
<_elemental> JDempster: well I've been trying to find some sort of explanation for how Registry solves some of the issues with Globals and why its a preferred method of dealing with global scope variables/objects. Most of what I have found has discussed that globals are bad and cause issues with...
<_elemental> code reuse and the like, but I don't see how the Registry solves this..

<JDempster> _elemental: variables made outside of functions and methods automatically use global space, sometimes there's a need for something to be global, to actually put it there though Zend_Registry is probably a better idea

<_elemental> JDempster: but why is it better to use the registry?

<ue> _elemental, becouse global variables are harder to manage

<JDempster> _elemental: for me, it's the actual action required to put it in the registry that makes it easier to use

<vi390> _elemental: clear, easy, objectorientated, getter setter, persistant namespace, and other Developer come around with it better

<_elemental> Ok I understand in theory how this helps. It makes the management and use easier to deal with in your head, but is there a code level reason? [I mean] how does using the registry to hold all of your globals help with reuse, debugging and understanding the code a year from now?

<_elemental> you still have to track down where the objects were set in the registry, like you have to track down where a global was declared...
you still have to ensure the object is in registry before you can grab it so reuse is still an issue

<JDempster> traking down where somthing is decleared globally is much harder todo than though a registry
you can implmenet lazy loading though the registry

<_elemental> so would it be against best practice to say load up a custom registry with default configurations for objects you tend to store there?
if you try to call something that isn't there the registry makes the default object?

<vi390> _elemental: its ONE place instead of many, and its sort of "best practice", If you need something you just get it from there, well its sort of a similar thing like Global, but you have better error management (IIRC is there an exeption thrown if you try to get an unset value, if you use global Variables it would just magicaly NOT WORK)

<JDempster> personally I think thats fine, extends the registry object, instance it, overload the __get to check that said object exists if not load default e.g. log or db connection what ever

<_elemental> I see to the whole point of the registry is just better management of the global space

<ue> _elemental, from a 2 year old php4 project (no oop) : global $_mysql,$_configs,$_module,$_tpl,$_var,$_tpl_index ,$_now,$_info,$_lang,$_lang_data, $_keywords, $_title; ... you want your code look like this ?

<Ezku_> _elemental: a registry + factory combination would be called a service locator, if i'm not mistaken

<vi390> ue: good point ;-)

<Ezku_> _elemental: and yes, it would indeed be good practice.

<_elemental> ue: ok, but how is that any better than $registry->set(...); $registry->set(..); .....

<vi390> or $_GLOBAL[][][][][][]

<ue> _elemental, the most simple is, you don't need to redecalre this globals in every class, just call registery

<vi390> _elemental: it definately makes the code more readable

<ue> _elemental, there also many other "tasty" things that registery pattern gives you, some of them already pointed out

<vi390> _elemental: maybe you find the advantages of using a registry in suing it for a while

<_elemental> ok so Registry gives you better managment of the global space, more readable code, the option to add validation and factory patterns to the global space and enhanced maintainability, anything else?

<vi390> _elemental: registries are sexy

* _elemental likes sexy
<JDempster> lol well theres the best reason then
<_elemental> :P
design:the_registry [phpPatterns]
Registry - Patterns For PHP
PHPit - Totally PHP Using globals in PHP
__________________
Zend Framework Resources: Zend Webinars | Reference Manual | API Docs | Books | FreeNode: #zftalk
Getting Started Tutorials: Getting started with ZF | Getting started with Zend Auth
Reply With Quote