I access my MySQL database via PDO. I\'m setting up access to the database, and my first attempt was to use the following:
The first thing I thought of is glob
Consider simply how your solution differs from the one presented in the PHP docs. In fact, there is just one "small" difference: your solution provides callers of the getter with a PDO
instance, while the one in the docs provides callers of Database::singleton
with a Database
instance (they then use the getter on that to get a PDO
instance).
So what conclusion do we reach?
Database
instance. The Database
class may expose (in fact, it should expose if you 're going to all this trouble) a richer or higher-level interface than the PDO
object it wraps.PDO
, then the two implementations are equivalent. There's no gain to be had from following the manual implementation.On the practical side, Singleton is a pretty controversial pattern. This is mainly because:
So, as a final conclusion: your singleton is just fine. Not using Singleton at all is just fine most of the time as well.