2017 © Pedro Peláez
 

library php-singleton

Singleton pattern for PHP

image

petrknap/php-singleton

Singleton pattern for PHP

  • Thursday, June 29, 2017
  • by petrknap
  • Repository
  • 1 Watchers
  • 2 Stars
  • 14,739 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 9 % Grown

The README.md

Singleton pattern for PHP

Simple implementation of singleton (anti-)pattern., (*1)

Why use a singleton?

Because it can solve deadlocks and other problems. See this example:, (*2)

class UnsafeFileAppender
{
    const MY_FILE = '/tmp/my.file';

    private $handle = null;

    public function __construct()
    {
        $this->handle = fopen(self::MY_FILE, 'a');
        flock($this->handle, LOCK_EX);
    }

    public function __destruct()
    {
        flock($this->handle, LOCK_UN);
        fclose($this->handle);
    }
}

You cannot create two instances at the same time with this code..., (*3)

$first = new UnsafeFileAppender();  // OK
$second = new UnsafeFileAppender(); // Deadlock

...so simply convert it into singleton..., (*4)

use PetrKnap\Singleton\SingletonInterface;
use PetrKnap\Singleton\SingletonTrait;

class SafeFileAppender extends UnsafeFileAppender implements SingletonInterface
{
    use SingletonTrait;

    private function __construct()
    {
        parent::__construct();
    }
}

...and use the same instance twice., (*5)

$first = SafeFileAppender::getInstance();  // OK
$second = SafeFileAppender::getInstance(); // OK

Run composer require petrknap/singleton to install it. You can support this project via donation. The project is licensed under the terms of the LGPL-3.0-or-later., (*6)

The Versions

29/06 2017

dev-master

9999999-dev https://petrknap.github.io/docs/php-singleton.html

Singleton pattern for PHP

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

06/01 2017

dev-backup/2017-03-12_master

dev-backup/2017-03-12_master https://github.com/petrknap/php-singleton

Singleton pattern for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

22/09 2016

v1.0.0

1.0.0.0 https://github.com/petrknap/php-singleton

Singleton pattern for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

05/03 2016

v0.1.0.4

0.1.0.4 https://github.com/petrknap/php-singleton

Singleton pattern for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

05/03 2016

v0.1.0.3

0.1.0.3 https://github.com/petrknap/php-singleton

PHP singleton by Petr Knap

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

27/02 2016

v0.1.0.2

0.1.0.2 https://github.com/petrknap/php-singleton

PHP singleton by Petr Knap

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

09/01 2016

v0.1-rc

0.1.0.0-RC https://github.com/petrknap/php-singleton

PHP singleton by Petr Knap

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires