piotrpress / singleton
This library is a Singleton (anti)pattern implementation using a Traits with a support for an Object Inheritance and passing parameters to the constructor.
v6.0.1
2022-02-22 15:33 UTC
Requires
- php: >=7.4
README
This library is a Singleton (anti)pattern implementation using a Traits with a support for an Object Inheritance and passing parameters to the constructor.
Installation
composer require piotrpress/singleton
Usage
require __DIR__ . '/vendor/autoload.php'; use PiotrPress\Singleton; class ExampleParent { use Singleton; } class Example extends ExampleParent { protected function __construct( $arg ) {} } Example::setInstance( 'arg' ); Example::getInstance();
Methods
setInstance()
- executes__construct()
and can be called only once, otherwiseException
will be throwengetInstance()
- returnsnull
before successfullysetInstance()
callissetInstance()
- returnstrue
if an instance exists,false
otherwiseunsetInstance()
- unsets an instance
Requirements
PHP >= 7.4
version.