okapi / singleton
PHP Singleton is a PHP library that allows you to create singleton classes with a trait.
Installs: 4 050
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: >=9.0
This package is auto-updated.
Last update: 2024-11-05 22:16:00 UTC
README
PHP Singleton
PHP Singleton is a PHP library that allows you to create singleton classes with a trait.
Installation
composer require okapi/singleton
Usage
<?php use Okapi\Singleton\Singleton; class GovernmentOfUSA { // Add the singleton trait use Singleton; /** * Function to register the singleton. * * This function and the "initialized" methods are completely optional. * * Can be static or non-static. */ public static function register(): void { // Get instance $instance = self::getInstance(); // For non-static just use $this // Make sure the instance is only registered once $instance->ensureNotInitialized(); // Do something // ... // Mark the instance as initialized $instance->setInitialized(); } /** * Custom function */ public function takeOverTheWorld(): void { // Make sure the instance is initialized $this->ensureInitialized(); // Do something // ... } } // Other file // Register the singleton GovernmentOfUSA::register(); // Take over the world $instance = GovernmentOfUSA::getInstance(); $instance->takeOverTheWorld(); // or GovernmentOfUSA::getInstance()->takeOverTheWorld();
Testing
- Run
composer run-script test
or - Run
composer run-script test-coverage
Show your support
Give a ⭐ if this project helped you!
📝 License
Copyright © 2023 Valentin Wotschel.
This project is MIT licensed.