Singleton¶
A singleton is a class which can only have one object. There is no way to instantiate the class and get a second, distinct object.
A singleton is used when there is a significant cost or limitations at maintaining multiple instances of the same object. For example, multiple Sqlite3
access to the same file, even as read-only, means several copies of the same file in memory. Or, several object referencing the same lock may be a problem.
Singleton are more difficult to test, as there is only one instance, and it is difficult to control it. Singleton may be achieve by dependency injection, with the same object.
<?php
// One method to build a singleton class
class Singleton {
private static self $instance = null;
// The constructor is private, to avoid multiple instantiation
// it is still accessible indirectly, via a public static method.
private function __construct()
{
// the usual constructo code
}
// the factory to build the singleton
public static function getInstance() : self {
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
}
?>
See also The little singleton, The singleton pattern::the good, the bad, and the ugly, Stop Using Singleton Pattern, PHP Design Patterns Game : The Singleton Pattern
Related : Design Pattern