Jump to content

Are 'registry' classes good practice?


JoeyH3

Recommended Posts

I'm trying to teach myself OOP PHP, and I've been looking up a number of tutorials on how to to make OOP MVC frameworks. I came across this one (cached version - their site is down at the moment of posting), and noticed something interesting about it that I've not seen in other tutorials.

 

Basically, it creates a registry class as $registry (which just uses the __set and __get functions to populate an initially empty array), and then stores variables in there. After that, $registry is passed on to the other classes via their __construct functions, so that they can access site-wide variables.

 

I think I understand how it's working, and see how it can be used, but this tutorial is the only time I've found such a thing. I was wondering, is using such a class good practice, or how should I store and access globally needed variables (like configuration, database connections, etc)?

Link to comment
Share on other sites

As long as this registry is injected via __constructs I don't see it as such a bad thing. It is basically a IoC container in that case.

 

Just make sure that you are type hinting to a well defined interface for this Registry.

 

The registry pattern has the potential to cause issue when you make it a singleton that has a static getInstance() method or similar.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.