A trait to implement the Singleton design pattern without any dependencies.
There already are plenty other singleton packages, so why another one? Simply, because most of those packages implement a factory, rather than a singleton pattern. This and many more things bothered me about them, so here is a simple, dependency-free and fully tested singleton.
$ composer require michaelspiss/singleton
One line is enough to turn a class into a singleton:
<?php
class StorageProvider {
use MichaelSpiss\DesignPatterns\Singleton;
...
}
To retrieve the singleton instance, simply call
$singleton = StorageProvider::getInstance();
There really isn't much left to say, but if you want to know more about constructors for singletons or forbidden actions you should check out the wiki!
- PHP >= 5.4
MIT