What are the potential limitations of using a Singleton pattern in PHP<5.3 and how can they be overcome?
Potential limitations of using a Singleton pattern in PHP<5.3 include lack of support for late static binding, making it difficult to subclass the Singleton class. To overcome this limitation, you can use a static variable to store the instance and access it through a static method.
class Singleton {
private static $instance;
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
// private constructor to prevent instantiation
}
}
// Usage
$singleton = Singleton::getInstance();