Registry Pattern Access Methods

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Registry Pattern Access Methods

Post by Ollie Saunders »

I've been using the registry pattern recently. I draw upon patterns for php's very nice article about them.

It has occurred to me that it might be nice to be able to store registries inside registries of course I realized this was entirely possible but then I considered how one my get data in and out of such a structure:

Code: Select all

OF::$registry->get('stdClasses')->register('blank', new stdClass());
OF::$registry->get('stdClasses')->get('blank');
The pattern that "patterns for php" described has the methods, register, unregister, has and get. But I thought why use register after set? Afterall set is more commonly known for setting things which is indeed what is happening when we register(). What is wrong in fact with, set, unset, isset and get? Or indeed __set(), __unset, __isset(), and __get()?

Of course this is making the registry appear to be like a stdClass and now you can access data like this:

Code: Select all

OF::$registry->stdClasses->blank = new stdClass();
OF::$registry->stdClasses->blank;
But is it still a registry? I say yes because my originial purpose for creating one is still valid. Here's an explaination of the purpose and extra features I have added to my registry:

Code: Select all

* OF_Registry can be used for two things. Its primary purpose is to faciliate
 * the storage of something, usually an object, under a label for later
 * retrieval. This is then improved by means of the has() method, a customizible
 * type limitation allow you to limit what enters the registry, and also checks
 * to prevent overrides/duplications of existing registry entries. OF_Registry's
 * primary purpose can be applied by the framework's user via a static instance 
 * of OF_Registry in the OF class called, funnily enough, $registry.
 *
 * The idea is that she/he may create an entities modify and then modify them in
 * some way by means of their public properties/methods and then store that 
 * modified version for later. This can then be retreived and cloned as required
 * ensuring that those changes you made don't have to be repeated or extended,
 * by means of a class, every time you require them.
 *
 * Don't forget, if you need to structure your registrations, that you can store
 * registries within a registry. Retrieval looks slightly odd but it does the
 * job:
 *      OF::$registry->get('fields')->set('txtFoo', new OF_TextSmall('txtFoo'));
 *      OF::$registry->get('fields')->get('txtFoo');
 *
 * The secondry purpose of the registry exploits its duplication checks. As it
 * is illegial (in HTML) and poteniually problematic (for client side scripting)
 * to create fields with the same id I wanted to enforce a way be which this
 * could not occur. I am able to use the registry as a means of storing labels
 * alone and use its duplication checks to throw an exception when a duplication
 * is created.
What I am saying is all that nice stuff I can do ^ up there ^ can be achieved with __set(), __get(), isset() and __unset(). So why for heavens sake weren't these magic methods used in the pattern in that article?

Oh and also do you like the sound of what I am doing?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Ultimately the accessors are really a matter of personal taste more than functionality. I would question widespread use of what are often called fluent interfaces unless some error handling scheme, probably exceptions, was implemented.
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I'm not sure I understand exactly what you're talking about, but I use a standard registry... with register, unregister, get, __get (which just wraps get), and __set (which just wraps register). Then I extend this class for more particular purposes.

for example, by session registry would override the register, unregister and get methods to set and retrieve values from $_SESSION.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

arborint wrote:Ultimately the accessors are really a matter of personal taste more than functionality. I would question widespread use of what are often called fluent interfaces unless some error handling scheme, probably exceptions, was implemented.
What kinds of errors are likely to occur?
Ninja wrote:__get (which just wraps get), and __set (which just wraps register)
Yeah I've done that now. Thanks.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

ole wrote:[What kinds of errors are likely to occur?
What happens to $registry->get('stdClasses')->get('blank') if the $registry->get('stdClasses') fails and returns null?
(#10850)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

you get an error
Post Reply