Singleton
Intent:
Ensure a single instance and provide a global access point
throughout the application’s use cycle
First, Singleton is not for database use. If you need to use multiple databases, what happens? Singleton becomes a problem. If you need a pattern
for database
, look for DAO
(for example). And if you don’t use it yet, try to work with PDO
in the PHP
, because it is much safer, flexible and meets well.
Second, Singleton in the PHP
is unfeasible. The reason? To each request http
made the PHP
automatically destroys all instances. Whatever language or technology is used, any situation where the answer destroys the instance singleton
makes no sense, because a new instance is generated at each request.
When using a pattern
you need to understand well what problem it proposes to solve, if it fits in the resolution of your problem and if the environment in which you will apply it will allow that purpose to be fulfilled. Anything other than this you should avoid using, because instead of solving your problem it will end up creating many others.
I recommend this as an additional reading: http://answall.com/questions/18860/por-que-n%C3%A3o-devo-usar-Singleton/18933#18933
– Papa Charlie
Video about Singleton in PHP: https://www.youtube.com/watch?v=hHKOLn0Lx7A
– user39153