Skip to main content

Singleton Pattern

Opensource ByJG Scrutinizer Code Quality Build Status

A lightweight PHP implementation of the Design Pattern Singleton using trait. Just one class and no dependencies.

Create your class

require "vendor/autoload.php";

class Example
{
// You need to use the trait here
use \ByJG\DesignPattern\Singleton;

// Put your code below
}

REMEMBER: Your class cannot have a public constructor. If it is necessary, use a private or protected constructor instead. Singleton classes does not have arguments in the constructor;

Use your class

$example = Example::getInstance();

Install

composer require "byjg/singleton-pattern=4.9.*"

References


Open source ByJG