Jump to content

Archived

This topic is now archived and is closed to further replies.

redbullmarky

*Stupid Question alert* - Registry

Recommended Posts

Hi all
I've just read my way through this: http://www.phpit.net/article/using-globals-php/1/ regarding using a registry to deal with globals. It's well written, and semi-laymans, but I need someone to go a step further, if possible.

1, can someone explain, in "PHP5 for Dummies" terms, exactly why I should use a registry?
2, doesn't it (like alot of other patterns i've seen) over complicate something that could be relatively simple?
3, is it a bandwagon for people to jump on to prove to others that they're a "proper" coder, to avoid the wrath of their "proper" peers?
4, unless i'm missing the point, all it does is replaces a few lines of 'global' with a few lines of OOP stuff, saying "globals are hard to work out what they're for and will break if you use this class again". But surely when 'delving' into the registry, you still need to know what's in there anyway, and if you don't set it to the registry your main code, the class that uses an object in the registry will break anyway? here's what i'm on about (excuse the bad pseudocode, but you should get my point):

The Rong Way (tm)
[code]
$db = new Database;

function test()
{
  global $db;

  // we can now use $db!!!
}
[/code]

The Correct Way (tm)
[code]
$registy = new Registry;

$db = new Database;
$registry->set('db', $db);

function test(&$registry)
{
  $db = $registry->get['db'];
}
[/code]

to me, it just seems like replacing one global space with another, which removes none of the issues. if i don't set 'db' in The Rong Way (tm), then my test don't work. if I don't set up the registry in The Correct Way (tm) then it don't work. what's the difference?

It's mad, because when I see alot of the responses to questions similar to mine, the normal response is "because it's the proper way, and that's that. [insert expert here] said so in his book."

Of course, I used to think the whole MVC was a load of rubbish but now I employ it (albeit in a very loose and "wrong" way), so I'm open to comments on this one.

Cheers
Mark

Share this post


Link to post
Share on other sites
I was using 'the wrong way' before, but have switched to using a registry.

The main reason I prefer a registry over globals is that it allows for more control. For example, in[url=http://www.phppatterns.com/docs/design/the_registry?s=registry] this example[/url] from phppatterns.com, they use a registry stack to enable restoring the registry to a previous state. Furthermore it can prevent unintentional 'overwriting' of objects, something that would probably take you a long time to debug. Applying a singleton pattern on all patterns that would qualify to put in a register can prevent this also though, when you declare the constructor private. So you wouldn't absolutely need a Registry if that were your only concern, at least not in PHP5.

I don't think using the globals array is really wrong, I look at a registry as an custimizable global array. I think its nice to have control over the way your global objects are stored and retrieved.

[quote]But surely when 'delving' into the registry, you still need to know what's in there anyway, and if you don't set it to the registry your main code, the class that uses an object in the registry will break anyway?[/quote]

The way I use it, when an object is requested that doesn't exist, a new one is created, returned, and added to the array of registered objects. Another example of control over retrieving global objects.


P.S. I don't think this is a stupid question at all. I agree one should never just adopt something just because it says it's a good idea in some book.

Share this post


Link to post
Share on other sites
Hi
Cheers for the response.
I had a good look through the article as well as [url=http://www.phpit.net/article/using-globals-php/3/]this[/url] one. I think my main problem with these is the fact that the reasons/justification all seems very 'programmed' (as if its a case of "its the correct way, and thats the way it is"). I do kinda understand why, but the only differences i can see by using this approach instead of globals are:

- because a method is used, rather than just setting a variable, checks can be done to make sure something is not being set again (overwritten).
- testing. in fact, the BIGGEST reason seems to be for testing purposes, rather than practical ones.

and that's pretty much it. so - apart from testing, when/why would i ever need to restore the registry to the previous state?

[quote]
The way I use it, when an object is requested that doesn't exist, a new one is created, returned, and added to the array of registered objects. Another example of control over retrieving global objects.
[/quote]
this sounds more like it. can you elaborate on this further (example)? how do you implement this?

[quote]
P.S. I don't think this is a stupid question at all. I agree one should never just adopt something just because it says it's a good idea in some book.
[/quote]
yeah exactly, but this is where alot of people's justification for these patterns,etc, comes from which is why they sound so 'programmed'. I gave MVC/templating the time of day and it proved useful, but in this case - I can kinda see why, but can also see easy alternatives, so i'm keeping an open mind on this.

Cheers
Mark

Share this post


Link to post
Share on other sites
That's the Factory Pattern when speaking of Instantiating non-existant Objects, phppatterns also have a page on that too, if memory serves.

You'll also find yourselves (both the OP and 448191) dabbling in the art of Dependency Injection before long, if you continue to use factories.

Share this post


Link to post
Share on other sites
now you've lost me lol
basically i'm in the process of restructuring a huge application (contact management, more specifically applicant tracking). however, i'm trying to build the basics so that it'll be easy for me to adapt/plug in/maintain as i go along. however, the nature of the packages interface is what's causing me the problems in terms of deciding the structure.

Jenk, if you'd be kind to spare a little time to advise/help me kick off the basics via MSN (30 mins tops) then that would be spot on.
Cheers
Mark

Share this post


Link to post
Share on other sites
No can do MSN :)

Have a read through Martin Fowlers website, and I highly recommend his Book Patterns of Enterprise Application Architecture (also known as PoEAA.) It describes just about every pattern you'll ever need (including MVC itself.)

Share this post


Link to post
Share on other sites
no worries.
i'm trying to keep things as simple as possible. whilst I don't doubt Martin Fowler's knowledge & practices, etc, from what I've read so far, I've not really found it easy to grasp mainly because i find it hard to associate with what i'm trying to do myself. In a nutshell, my main system works as a "desktop", with the modules running in seperate windows (possibly iframes in a style DIV, and basically a script/DOM of their own which causes other issues for me such as interraction between different modules). so as well as considering the structure of these modules, obviously i also have to consider how these modules can work with eachother as well as still being able to interract with the main 'core' of the program, etc. Ok so that's not directly involving patterns yet, but it's still part of trying to nail a good structure for the whole system that allows me to do stuff like this - ie, a browser based, "multitasking" Contact Management system. "multitasking" being the biggest issue at the moment.

Cheers
Mark

Share this post


Link to post
Share on other sites
[quote author=redbullmarky link=topic=108613.msg437813#msg437813 date=1158746996]
..but the only differences i can see by using this approach instead of globals are:

- because a method is used, rather than just setting a variable, checks can be done to make sure something is not being set again (overwritten).
- testing. in fact, the BIGGEST reason seems to be for testing purposes, rather than practical ones.
[/quote]

I would dissagree that testing is the biggest reason to use a Registry, I think the biggest reason is control in general. Both your points above fit into that category. If you want to compare these two, I personally think point one is more important.

But like I said, that (in PHP5) can also be accomplished with Singletons.

[quote author=redbullmarky link=topic=108613.msg437813#msg437813 date=1158746996]so - apart from testing, when/why would i ever need to restore the registry to the previous state?[/quote]

I can't think of reason. If you wanted to display a previous state of the application you're better off facilitating it to be read from cache.

[quote author=redbullmarky link=topic=108613.msg437813#msg437813 date=1158746996]
this sounds more like it. can you elaborate on this further (example)? how do you implement this?
[/quote]

It's really simple. When some class tries to access a non-existant index, the registry attempts to create an object with that name.

[code]<?php
function get($key) {
if(!isset($this->registeredObjects[$key])){
//Index not found, try creating.
if(!$obj = new $key){
      return null;
}
$this->set($key, $obj);
return $obj;
}
return $this->registeredObjects[$key];
}
?>[/code]
In the most general sense I guess it's a Factory Method pattern.
__autoload() handles the inclusion of the correct file, and returns false if the file doesn't exists. I don't really see any need (yet) for for a full blown Service Locator.

Share this post


Link to post
Share on other sites
That will automatically load all dependencies that are in the objects constructor, and their dependencies, and their dependencies etc. etc.

You'll see soon enough if you continue to use your Factory why it's useful.

As for the overhead, I seriously suggest you step away from worrying about such trivial affairs as overheads of one extra object, or which output method to use. They really are only minor issues.

I'd also appreciate you don't go copying and pasting my code all over the place - especially without adding the comments that accompanied that post ;)

Share this post


Link to post
Share on other sites
[quote author=Jenk link=topic=108613.msg437896#msg437896 date=1158757411]
That will automatically load all dependencies that are in the objects constructor, and their dependencies, and their dependencies etc. etc.

You'll see soon enough if you continue to use your Factory why it's useful.
[/quote]

I'll take your word for it. Don't see any use for it right now.

[quote author=Jenk link=topic=108613.msg437896#msg437896 date=1158757411]
As for the overhead, I seriously suggest you step away from worrying about such trivial affairs as overheads of one extra object, or which output method to use. They really are only minor issues.
[/quote]

'Overhead' is one of my main concerns since I started to get more serious about OOP. I'm not worried about a single extra object, I just feel you can easily make your framework too 'bulky'. Extra processing === less perfomance, trivial if done a couple of times, a problem if done many times.

[quote author=Jenk link=topic=108613.msg437896#msg437896 date=1158757411]
I'd also appreciate you don't go copying and pasting my code all over the place - especially without adding the comments that accompanied that post ;)
[/quote]

I'll post a link instead if there comes a next time, no worries.  :)

http://forums.devnetwork.net/viewtopic.php?p=309081#309081

Share this post


Link to post
Share on other sites
A Quick example..

In your Registry with Factory - what happens if you request an object that has a dependency?

At the moment, the Factory attempts to instantiate the object without passing any arguments, if your object requires the use of, for example, a database object:
[code]<?php

class SomeClass
{
    public function __construct(DatabaseClass $db)
    {
        ...
    }

    ...
}
?>[/code]

You'll get an error with your application. In steps dependency injection, to inject that DatabaseClass object.

P.S. edit your previous post, thanks.

Share this post


Link to post
Share on other sites
[quote author=Jenk link=topic=108613.msg437946#msg437946 date=1158760846]
A Quick example..

In your Registry with Factory - what happens if you request an object that has a dependency?

At the moment, the Factory attempts to instantiate the object without passing any arguments, if your object requires the use of, for example, a database object:
[code]<?php

class SomeClass
{
    public function __construct(DatabaseClass $db)
    {
        ...
    }

    ...
}
?>[/code]

You'll get an error with your application. In steps dependency injection, to inject that DatabaseClass object.

P.S. edit your previous post, thanks.
[/quote]
i'll probably regret being so naive when a problem slaps me round the face with a wet fish, but I honestly don't understand the purpose/complexity of patterns.
With a function, for example, i pass in some parameters and out comes my result. what goes INSIDE the function is irrelevent as long as it works.
With the few classes i've written so far, they have a constructor and methods. i set up an instance, do my thing, and get the results I need. And nothing stops me using my functions and classes again in other projects without changing anything, and they are designed to have no outside dependencies.

So what, in laymans, is the purpose of all the extra complexity of 'dependancies', layering, etc? Am i missing something?

Share this post


Link to post
Share on other sites
Patterns are repeated practices that are also common practices.

Developers are lazy. I don't want to have to construct a new class for each and every problem/pattern in each and every application I make. It get's boring fast, and more importantly, uses up a lot of my time. Time == money. You could argue "I'll just charge that extra time to my customer." but you'll find that the customer might not be so generous, and go sign the dotted line to someone else who is far more efficient and will get their application up quicker, thus saving them money.

So to be efficient, I can identify patterns and create classes that can achieve the purpose those patterns. I can now reuse my classes in many applications, saving me all the hassle of rewriting application specific classes.

Share this post


Link to post
Share on other sites
[quote author=Jenk link=topic=108613.msg438051#msg438051 date=1158766210]
post still not edited..
[/quote]

What exactly would you like me to edit?  ???

I honored your 'request' to remove your code as I don't want to republish other peoples code without permission (it certainly wasn't because you asked so nicely).

So I guess I have to ignore this very kindly worded and descriptive request (since I don't have a clue what you're talking about).

Share this post


Link to post
Share on other sites
Your post was not edited at the time of me making that post ;)

Share this post


Link to post
Share on other sites
[quote author=Jenk link=topic=108613.msg438556#msg438556 date=1158825751]
Your post was not edited at the time of me making that post ;)

[/quote]

Hmm, ok. I could have sworn that post was added after I edited. Maybe you were writing while I was editing, it doesn't really matter... Ok, it doesn't matter at all... :P

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.