Is it okay for a registry object to contain a set of singletons?

Hey guys,

I’m just wondering if having a registry object that contains references to a group of different purpose singleton objects makes sense or defeats the purpose of having a registry at all. Bear in mind, I’m asking this as someone who’s starting to sort of understand the MVC principle.

So, let’s say I have a Registry object. I also have a bunch of singleton objects of classes Router, Initialiser, DB etc as I don’t want to ever have more than instance of each. If the Registry object created instances to each like this…

$this->db= DB::instance();
$this->router = Router::instance();

// etc.

… is that okay?

Thanks guys for the replies. I have seen the light :smiley:

I dislike the singleton pattern simply because it reduces the flexibility of your application to just about 0.

My reasons for prefering dependency injection are the following:

  • Multiple data sources may be used in one. Whilst the benefit may not immediately be relevant or obvious, but I’ve worked on such requirements and singleton applications require much more code to implement it.
  • A main application may launch a separate sub-application to delegate work. For example, you can place ‘modules’ inside your application which work independently of each other. Singleton applications don’t allow multiple application objects like this, especially if they use global setting objects (hence the name singleton).
  • Polymorphism. If I want to extend an object so that it behaves differently, I simply need to pass that to the application, rather than it using the default all the time.

My major classes - the Application, Request and Database classes, have methods to generate the default object, for example the default database object defaults to the default database connection settings. HOWEVER, this is only used in the initial startup of the application, basically to make the index.php shorter I suppose.

This maybe interesting…