web6 / singleton
PHP Singleton design pattern
0.0.1
2019-05-19 21:23 UTC
This package is auto-updated.
Last update: 2025-03-23 06:16:16 UTC
README
Implementation of singleton design pattern in PHP5.4+ using a trait.
Install
Install via Composer
$ composer require web6/singleton
Usage
Configure autoload
Configure autoloading by including Composer's generated file :
include_once('vendor/autoload.php');
Create a singleton class
To create a singleton class simply use the W6\Sinfleton\SingletonTrait
and move the __construct()
logic to the init()
method.
class App { use \W6\Singleton\SingletonTrait; public $message = 'Not inited'; protected function init() { $this->message = 'Inited'; } }
Use your class
Anywhere in your application you can request the same instance of the class.
$app = App::instance(); echo $app->message;