lianhua/singleton

This package is abandoned and no longer maintained. No replacement package was suggested.

A simple PHP class for singleton

2.0 2020-04-08 15:11 UTC

This package is auto-updated.

Last update: 2023-05-29 01:32:57 UTC


README

Build Status BCH compliance

Overview

A simple PHP class for singleton

Compatibility

This library has been tested for PHP 7.3 and higher

Installation

Just use composer in your project:

composer require lianhua/singleton

Usage

Create a class using \Lianhua\Singleton\Singleton trait, that's all.

class MySingleton
{
    use \Lianhua\Singleton\Singleton;
    // Your methods and properties here
}

If you need a constructor, make sure it's a protected one

class MySingleton
{
    use \Lianhua\Singleton\Singleton;

    private $n;

    protected function __construct()
    {
        $this->n = 0;
    }
}

Upgrading

From 1.0 to 2.0

Singleton became a trait instead of a class in order to create many singleton classes. Please edit your class like this:

Then

class MySingleton extends \Lianhua\Singleton\Singleton
{
    private $n;

    protected function __construct()
    {
        $this->n = 0;
    }
}

Now

class MySingleton
{
    use \Lianhua\Singleton\Singleton;

    private $n;

    protected function __construct()
    {
        $this->n = 0;
    }
}