Skip to content

This library is a Singleton (anti)pattern implementation using a Traits with a support for an Object Inheritance and passing parameters to the constructor.

License

PiotrPress/singleton

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.

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, otherwise Exception will be throwen
  • getInstance() - returns null before successfully setInstance() call
  • issetInstance() - returns true if an instance exists, false otherwise
  • unsetInstance() - unsets an instance

Requirements

PHP >= 7.4 version.

License

GPL3.0

About

This library is a Singleton (anti)pattern implementation using a Traits with a support for an Object Inheritance and passing parameters to the constructor.

Topics

Resources

License

Stars

Watchers

Forks

Languages