PHPackages                             petrknap/php-singleton - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. petrknap/php-singleton

Abandoned → [petrknap/singleton](/?search=petrknap%2Fsingleton)Library[Utility &amp; Helpers](/categories/utility)

petrknap/php-singleton
======================

Singleton pattern for PHP

v2.0.2(1y ago)4140.7k↓46%212LGPL-3.0-or-laterPHPPHP &gt;=8.1

Since Jan 9Pushed 1y ago1 watchersCompare

[ Source](https://github.com/petrknap/php-singleton)[ Packagist](https://packagist.org/packages/petrknap/php-singleton)[ Docs](https://github.com/petrknap/php-singleton)[ Fund](https://petrknap.github.io/donate.html)[ RSS](/packages/petrknap-php-singleton/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (5)Versions (11)Used By (12)

Singleton pattern for PHP
=========================

[](#singleton-pattern-for-php)

Simple implementation of singleton (anti-)pattern.

Why use a singleton?
--------------------

[](#why-use-a-singleton)

Because it can solve deadlocks and other problems. See this example:

```
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...

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

...so simply convert it into singleton...

```
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.

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

---

Run `composer require petrknap/singleton` to install it. You can [support this project via donation](https://petrknap.github.io/donate.html). The project is licensed under [the terms of the `LGPL-3.0-or-later`](./COPYING.LESSER).

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance46

Moderate activity, may be stable

Popularity36

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~483 days

Recently: every ~831 days

Total

8

Last Release

401d ago

Major Versions

v0.1.0.4 → v1.0.02016-09-22

v1.0.0 → v2.0.02024-03-12

PHP version history (2 changes)v0.1-rcPHP &gt;=5.4

v2.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/c0e19f2ea17567eb9f536d1e00595b83630dfb8b2df6e7e422e06a9a48e85585?d=identicon)[petrknap](/maintainers/petrknap)

---

Top Contributors

[![petrknap](https://avatars.githubusercontent.com/u/8299754?v=4)](https://github.com/petrknap "petrknap (10 commits)")

---

Tags

design-patternphpphp-librarysingletonsingleton-patterndesign patternsingletonsingleton pattern

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/petrknap-php-singleton/health.svg)

```
[![Health](https://phpackages.com/badges/petrknap-php-singleton/health.svg)](https://phpackages.com/packages/petrknap-php-singleton)
```

###  Alternatives

[league/pipeline

A plug and play pipeline implementation.

1.0k16.0M74](/packages/league-pipeline)[florianwolters/component-util-singleton

The Singleton (and Registry of Singletons a.k.a. Multiton) design pattern as a PHP component.

13175.8k5](/packages/florianwolters-component-util-singleton)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
