michaelspiss/singleton

A trait to implement the Singleton design pattern without any dependencies.

v1.0.1 2017-04-21 11:58 UTC

This package is not auto-updated.

Last update: 2024-04-14 00:59:43 UTC


README

A trait to implement the Singleton design pattern without any dependencies.

Build Status Coverage Status

Why?

There already are plenty other singleton packages, so why another one? Simply, because most of those packages implement a factory, rather than a singleton pattern. This and many more things bothered me about them, so here is a simple, dependency-free and fully tested singleton.

Installation

$ composer require michaelspiss/singleton

Basic Usage

One line is enough to turn a class into a singleton:

<?php

class StorageProvider {
    
    use MichaelSpiss\DesignPatterns\Singleton;
    
    ...
}

To retrieve the singleton instance, simply call

$singleton = StorageProvider::getInstance();

Read more

There really isn't much left to say, but if you want to know more about constructors for singletons or forbidden actions you should check out the wiki!

PHP Requirements

  • PHP >= 5.4

License

MIT