Today's snippet describes the singleton pattern. The singleton pattern is a method of creating a class that statically maintains an instance of itself and ensures that no other instances can be created. This is useful for classes that implement app-wide services such as logging, DB access and other shared resources.
The basic requirements for a singleton class are as follows...
- The constructor is private
This ensures that nothing outside the class can create instances.
- A static method exists that returns the instance
When first called the method creates a statically stored instance of the class. Subsequent calls return the existing instance.
- Magic methods to prevent the instance from being cloned or serialized (PHP-specific)
PHP provides functionality to clone and serialize objects. By adding simple implementations of the clone and wakeup magic methods we can prevent this from happening.
That's basically it. Here's a simple example...
This is an extremely simple example but it demonstrates all of the core concepts. You use this class as follows...
If you attempt to clone or unserialize an instance your script will fail with an error.
I use this pattern all over the place and definitely recommend using it where it makes sense in your applications. An alternative would be to implement the entire class statically and to be honest I'm not sure whether there are advantages to either implementation. If someone knows please share in the comments.
That's it for this snippet, stay tuned for more. As always comments, questions, suggestions and requests are welcomed.