views:

86

answers:

1

I have asked a few question lately about the use of the singleton and registry patterns but here is a new question I have.

Let's say I have 3 main class object in PHP that I need to have access to inside 20 other class files and sitewide.

  • Database object
  • Cache object
  • Core object

I then can use a Registry pattern to store all 3 of these object into 1 registry object which we will call the "Registry object"

The registry class will have a singleton method so that we can make sure we only call the Registry object 1 time.

So far all is good, here is where I need your advice. All the 20 other classes now have access to the 3 main objects I mention above. But inside those 3 object above, is it ok to have them include the registry object too? Because they will need access to each other as well, or should I just reference a singleton method for the database, cache, and core object inside of each of these 3? This may be a dumb question and I am probably just a little confused because the singleton from the registry object probably makes it ok to include the reistry inside the objects that make up the registry?

A: 

Why don't you just make these 3 classes static? I'm using a static database class that kind of extends PHP's PDO class and let me use it like this:

$sql = "SELECT * FROM table";
foreach(DB::query($sql) as $row) {
  // do something
}

I really like this way of using static classes/functions especially for this "global" kind of stuff. Perhaps this is an option for you too?

Thomas
It is an option but I am already going to be using a registry pattern to store and access other objects and variables into so I was thinking I might as well just access my DB object with that I think.
jasondavis
But the Registry Pattern describes afaik a static Registry class so that every class can use it. So there is no "Registry object", just the static stuff an every class can use the Registry. I'm referencing to this: http://www.phpbar.de/w/Registry
Thomas