lianhua / singleton
A simple PHP class for singleton
Installs: 25
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/lianhua/singleton
Requires (Dev)
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2023-05-29 01:32:57 UTC
README
Overview
A simple PHP class for singleton
Compatibility
This library has been tested for PHP 7.3 and higher
Installation
Just use composer in your project:
composer require lianhua/singleton
Usage
Create a class using \Lianhua\Singleton\Singleton trait, that's all.
class MySingleton { use \Lianhua\Singleton\Singleton; // Your methods and properties here }
If you need a constructor, make sure it's a protected one
class MySingleton { use \Lianhua\Singleton\Singleton; private $n; protected function __construct() { $this->n = 0; } }
Upgrading
From 1.0 to 2.0
Singleton became a trait instead of a class in order to create many singleton classes. Please edit your class like this:
Then
class MySingleton extends \Lianhua\Singleton\Singleton { private $n; protected function __construct() { $this->n = 0; } }
Now
class MySingleton { use \Lianhua\Singleton\Singleton; private $n; protected function __construct() { $this->n = 0; } }