michaelspiss / singleton
A trait to implement the Singleton design pattern without any dependencies.
Requires
- php: >= 5.4
Requires (Dev)
- phpunit/phpunit: ^4.1
This package is not auto-updated.
Last update: 2025-03-30 06:09:24 UTC
README
A trait to implement the Singleton design pattern without any dependencies.
Why?
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.
Installation
$ composer require michaelspiss/singleton
Basic Usage
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();
Read more
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 Requirements
- PHP >= 5.4
License
MIT