Skip to content

w6cloud/php-singleton-trait

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WEB6 PHP Singleton Trait

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;

About

PHP singleton design pattern

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages