How to use the singleton design pattern in php5
PHP5 is a long way removed from PHP4, at least in terms of syntax. While it's still possible to code in a procedural style, those coming from a Java background will be pleased to see that some of the same constructs are available. One of those, commonly found in Java code, now makes an appearance - the singleton.
What is a singleton in PHP?
The scope of variables in PHP is different when run under Apache in the usual way. The maximum scope of a variable is basically 'request' scope, unless you make use of sessions. There is no 'container' or 'application' scope like you have in Java, thus removing one of the key advantages of the singleton pattern. In Java, a singleton means there is only ever one instance in a JVM. In PHP, a new object will be created per request. So, connection pools are still out of the question. However, for complex applications, a singleton can still be useful in PHP, for logging, timing, or ensuring a single instance of anything exists during some processing.
Example
1:
2:/**
3: * @package example
4: * @author Mick Sear, eCreate Web Services Limited
5: */
6:
7:/**
8: * Singleton class to demonstrate factory methods and singleton design pattern in PHP5
9: */
10: class ThereCanBeOnlyOne
11: {
12:
13: /** Store the instance of the class */
14: private static $instance;
15:
16: private function __construct()
17: {
18: }
19:
20: public static function getInstance()
21: {
22: //__CLASS__ is always resolved to this class, even if subclassed
23: if (!self::$instance instanceof __CLASS__)
24: {
25: self::$instance = new ThereCanBeOnlyOne();
26: }
27: return self::$instance;
28: }
29:
30: public function __clone()
31: {
32: throw new Exception("Cloning of this object is not supported.");
33: }
34: }
35:
36:?>
What this code does is to utilise a couple of new PHP constructs.
- Firstly, we've got the private and public visibility modifiers. These are used here to prevent the class from being constructed externally and to protect the $instance variable.
- We've made the $instance variable static, and the getInstance() method static so they don't require an instance of the class before they can be used.
- The self:: construct allows access to the static members of the class, since there is no '$this' context here.
- The magic variable __CLASS__ refers to the name of the class in which it is defined, so even if this is subclassed, __CLASS__ refers to 'ThereCanBeOnlyOne',
- The PHP5 magic methods __construct and __clone are used.
- Finally, we throw an exception in __clone.
So, when we call ThereCanBeOnlyOne::getFactory() multiple times in the scope of a request, we can be sure that we're returning the same instance.
Latest articles
- Google scores!
- Mandriva back in my top list
- The perils of package managers
- Why mix PHP and Java?
- An InputStream Iterator in Java
- Pagination in PHP or Java
- PHP and Java integration with Quercus
- Trying out Zend Studio for Eclipse
- Which framework for a new CMS?
- How to create a singleton in PHP
- A brief foray into MySQL Stored Procedures
- Sun VirtualBox vs. VMWare
- We're now a four computer household