Singleton
Intention:
Ensure a single instance and provide a global access point
to it throughout the application usage cycle
First, singleton is not for database use. If you need to use multiple databases, what happens? Singleton becomes a problem. If you need pattern
to database
, search for DAO
(for example) . And if you do not use it yet, try working with PDO
on PHP
, as it is much safer, more flexible and serves well.
Second, singleton in PHP
is impracticable. The reason? At every request http
made PHP
automatically destroys all instances. Whatever the language or technology used, any situation in which the response destroys the instance singleton
ceases to make sense, because a new instance is generated for each request.
When using a pattern
you need to understand well what problem it intends to solve if it fits the resolution of your problem and whether the environment in which you will apply it will allow that purpose to be fulfilled. Anything other than this you should avoid using, as opposed to solving your problem it will end up creating many more.