I have recently been reading a little about singleton patterns. When it reads its technical aspects, it is ideally suited to manage the database handler or choice. But after reading the vast resources it appears that the developer community does not really favor the pattern.
I am struggling to find a better solution to such a problem - that is, only one handler can be started at a time - so why is the pattern so bad? Is it overloaded or is it basically defective?
Php is the language I am using.
The design pattern was made for languages where the global variables are difficult or impossible, or where they Bad behavior is considered. (In fact, common design patterns are designed for restrictive languages. A large number of them are simply unnecessary in other languages.)
PHP is the global variable in PHP Global variables usually a poor practice But if you want to use them, then they exist.
However, there are some reasons that you want singleton in PHP.
Singletons are useful when calling to getInstance (the canonical name for the method that gives a single example of singleton) I was created at any point in the script. To that point, the object is not required for existence if the object was instead of a global variable, either it already exists or the code trying to refer to the object will need to be prompted in advance, in fact, wherever It can be used, it will need to get started right away, by centralizing the creation of a single object in getInstance , you can specify the object Rbhit to each copy-paste to avoid creating boilerplate.
The database object usually requests are prepared very quickly in the lifetime, which will ruin the specific benefit of singleton-ras.
There are other options of singletones that can get jobs in other ways. An example is a fancy term for passage of external objects that relies on the object at the time of creating a new object (such as database handles). However, it can be complicated or disturbing, correcting it may involve all the time in the injection of lot of the same object.
There is another option, which is a container for other things that would otherwise be global if you do not like the global variable, but they are being effectively named, then it will be a solution that You wish
Finally, choose a way to do this, and stick it privately with one side of my codebase, I am a global fan of database objects.
Comments
Post a Comment