cse / base-singleton
A Singleton Design Pattern which allow easy used singleton class.
Requires
- php: >=7.1
- cse/base-exceptions: *
Requires (Dev)
This package is auto-updated.
Last update: 2024-10-19 03:22:25 UTC
README
English | Русский
SINGLETON CSE BASE
A Singleton Design Pattern which allow easy used singleton class.
Project repository: https://github.com/cs-eliseev/base-singleton
DEMO
class ExampleSingleton { use SingletonTrait; ... } $instance = ExampleSingleton::getInstance(); $instanceName = ExampleSingleton::getInstance('instance_name');
Introduction
CSE BASE is a set of libraries written in PHP specifically to extend your classes.
A set of basic methods for creating your classes is what you need to quickly create web applications. SINGLETON CSE BASE, позволяет легко и просто создавать singleton класс.
CSE BASE was created for the rapid development of web applications.
CSE Base project:
Below you will find some information on how to init library and perform common commands.
Install
You can find the most recent version of this project here.
Composer
Execute the following command to get the latest version of the package:
composer require cse/base-singleton
Or file composer.json should include the following contents:
{ "require": { "cse/base-singleton": "*" } }
Git
Clone this repository locally:
git clone https://github.com/cs-eliseev/base-singleton.git
Download
Download the latest release here.
Usage
View test model: ModelSingleton.php
See examples: examples-singleton.php
Create Model Singleton
Examples:
class ModelSingleton { use SingletonTrait; protected $param = 0; /** * @param int $param */ public function setParam(int $param): void { $this->param = $param; } /** * @return int */ public function getParam(): int { return $this->param; } }
Create INSTANCE singleton
Examples:
$instance = ModelSingleton::getInstance(); $instance->setParam(10); $instance->getParam(); // 10
Create singleton instance by name:
$instance2 = ModelSingleton::getInstance('new'); $instance2->setParam(20); $instance->getParam(); // 10 $instance2->getParam(); // 20
Restore singleton instance:
$instance3 = ModelSingleton::getInstance(); $instance3->setParam(30); $instance->getParam(); // 30 $instance2->getParam(); // 20 $instance3->getParam(); // 30
EXCEPTIONS singleton
__CLONE:
try { $clone = clone ModelSingleton::getInstance(); } catch (CSESingletonException $e) { // Singleton can not using clone }
__SLEEP:
try { $serialize = serialize(ModelSingleton::getInstance()); } catch (CSESingletonException $e) { // Singleton can not serialize }
__WAKEUP:
try { ... } catch (CSESingletonException $e) { // Singleton can not deserialize }
Testing & Code Coverage
PHPUnit is used for unit testing. Unit tests ensure that class and methods does exactly what it is meant to do.
General PHPUnit documentation can be found at https://phpunit.de/documentation.html.
To run the PHPUnit unit tests, execute:
phpunit PATH/TO/PROJECT/tests/
If you want code coverage reports, use the following:
phpunit --coverage-html ./report PATH/TO/PROJECT/tests/
Used PHPUnit default config:
phpunit --configuration PATH/TO/PROJECT/phpunit.xml
Donating
You can support this project here. You can also help out by contributing to the project, or reporting bugs. Even voicing your suggestions for features is great. Anything to help is much appreciated.
License
The SINGLETON CSE BASE is open-source PHP library licensed under the MIT license. Please see License File for more information.
GitHub @cs-eliseev