Registry Pattern

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
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Registry Pattern

Post by feyd »

Although I've found lots of vague descriptions of what a Registry Pattern is and does (Fowler didn't help much in PoEEA), I can't quite figure out how to go about creating the interface and/or implementation.. it seems to me that it's just a named property bucket.. :? Here's the really rough idea of what I'm seeing one as:

Code: Select all

class TRegistry {
  private $FRegistry;

  function __construct() {
    // TODO add constructor logic, if any
    $this->$FRegistry = array();
  }

  function __destroy() {
    // TODO add destruction logic, if any
  }

  function AddItem($name,$value) {
    while(isset($this->FRegistry[$name])) {
      $name = uniq_id();
    }
    $this->FRegistry[$name] = $value;
    return $name;
  }
}
overload the __get and probably __set to make it "easier"... would have to overload __call too :roll: damn calling methods.. oh well..
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Post by dbevfat »

yes, it's basically a "named property bucket", but you can specialize it into a "specifical properties bucket" by getting rid of more general Get/Set in favour of more specific getters/setters, like GetDatabase/SetDatabase, GetUser/SetUser ...

Also, you can put more specialization to it, like denying writing to properties that were already set (allow first write only).

But you must store them in such manner that you can read them afterwards, so using uniq_id() is not the way to go. You'd be better off storing them in a hash with their name as the index, this way the reading would work, too.

Code: Select all

function get($name)
{
  if (isset($this->FRegistry[$name]))
    return $this->FRegistry[$name];
  else
    return null;
}

function set($name, $value)
{
  $this->FRegistry[$name] = $value;
}
edit + offtopic: i see you use pascal (delphi) naming conventions, noticed the T for class and F for private member preffix. :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hmm okay.. it was just bizarre that its talked about all over (sorta) but no one could seem to hammer down an actual interface for me to understand :lol:

So now I see it as a theory pattern and not so much an actual finite pattern.. it'd be nice if someone would say that. :lol:
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

it seems to me that it's just a named property bucket..
Aha...;) This must be one of the worst described patterns ever...its a good runner. I'm not even sure it's always put in the right context when described. Last implementation I saw was basically storage for singletons...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

So this pretty much just makes a unique variable for you to use? (I've not been in a position to need to know this stuff, so I am asking as I am learning :P)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

feyd wrote:hammer down an actual interface for me to understand
With a singleton Registry you get easy global access to registry properties:

Code: Select all

class Foo
{
    function Foo()
    {
        $registry =& Registry::getInstance();
        $this->quark = $registry->getQuark();
        $this->strangeness = $registry->getStrangeness();
        $this->charm = $registry->getCharm();

       // etc
You don't have to use named getters although I think I'd prefer that myself.

As Fowler says "global data is always guilty until proven innocent" so you'd probably want to hold off until the pressure mounts ie passing stuff around a lot through methods which are merely handing over to other methods, & etc. A singleton registry can be used as a ServiceLocator.

Also note the static reference problem.

Unfortunately phppatterns.com is down right now (I think there are plans either to restore the site or to get the content up somewhere else). Marcus Baker had an article on the Registry pattern here, including a version adapted to allow testing - obviously it's hard to create and destroy multiple instances of a singleton in a single test script. I'm afraid I can't remember how he did it.

It kind of feels like OOP's dirty little secret doesn't it? The whole point of encapsulation is to restrict scope. At least it gives you a bit more control than simply using global vars.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

phppatterns.com is back up! :D
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Great!
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Are you sure? I couldn't reach it...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it was up yesterday :?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Jenk wrote:So this pretty much just makes a unique variable for you to use? (I've not been in a position to need to know this stuff, so I am asking as I am learning :P)
:oops:
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

It's down! :?
Post Reply