Is there anything wrong with registry design design?

Is a registry design pattern a good solution for this in PHP?

For a social networking site (facebook, myspace).

let's say I have a database class that creates a single database connection and allows me to do database material and a Session class that allows me to process sessions, as well as a Cache class that allows me to cache items and retrieve them. So, these are 3 main classes that I will need to have access to on every page of my site. After reviewing the registry template in the last hour, I think this is the perfect solution. I can store database, session, and cache objects in a registry object, and then embed a registry object on every page or every other class and have access to my database, sessions, and cache.

Before that, I used a singleton template, so I would have to call my singleton method for all three of my MAIN classes inside each page or another class.

So, I'm just wondering if there are problems using the Registry class? 1, what I can see seems to be harder to see which classes depend on other classes and the like. Another thing is that this seems like a great solution for this, I also saw another publication here about the user registry class, where they saved settings in the registry, having access to them in all other classes into which the registry object was transferred. I’m sure I’ll find good use for this function as well.

So, the only question is: did I miss something or just warmed up the lotto?


UPDATE

Also, if you use the registry to store objects, should I do something like this ...

$this->session = $registry->getObject('session'); 

or instead

 $this->registry->session = $registry->getObject('session'); 

The second way seems that it might be easier to understand where the object came from?

+4
source share
1 answer

You mean the Singleton s registry, right? If so, it looks like all you really need is access to three global objects everywhere, but contained inside another object.

So, if you wrote cache.php, sessions.php, and database.php files that define the Cache , Sessions and Database classes, you want to contain them all inside the Registry object defined in registry.php.

First of all, it's great to control ORDER instantiations in this way. This is better than just doing require_once cache.php, sessions.php and database.php, and inside them you define not only a class, but also one global instance. This controls the instance ORDER by the way you include / require it. The look is sleazy. It is better to have your own registry object, which, when it is created and becomes a global $registry , the first thing is to control the creation of your global symbols in the order and how you want.

Secondly, it is great to have one Registry.php, which has one global registry $. Defining global variables here and there from various files is difficult.

Now that I have agreed with you, I ask you an important question. As you can see, they are different:

 $registry->getObject('session'); $registry->getObject('database'); $registry->getObject('cache'); 

vs

 $registry->getSession (); $registry->getDatabase (); $registry->getCache (); 

Personally, I like the latter. You do not use the string "session" to refer to the Session object obtained through the super-generic getObject . Instead, you use getSession() to get the session. He reads better.

Your registry, in the end, knows everything about the three global steps, it creates them explicitly, so it is already locked for one purpose. Adding specific methods, also related to its sole purpose, is not “weak” or “bad”. Instead, I think it's less code and easier on eyeballs.

-one
source

All Articles