PHPackages                             delights/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. delights/singleton

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

delights/singleton
==================

Utilities to work with the singleton pattern

1.0.0(6y ago)111MITPHPPHP ^7.4

Since May 6Pushed 5y ago1 watchersCompare

[ Source](https://github.com/felixdorn/Singleton)[ Packagist](https://packagist.org/packages/delights/singleton)[ RSS](/packages/delights-singleton/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

 [ ![](https://camo.githubusercontent.com/12740cef77275868ec33992d2342c61f1366a4763cf9fa55102fd87cfcd3ee02/68747470733a2f2f7265732e636c6f7564696e6172792e636f6d2f6479336a78686962612f696d6167652f75706c6f61642f76313538383638343932382f6d6170732d616e642d6c6f636174696f6e5f315f6e77727a747a2e737667) ](https://github.com/felixdorn/singleton)

 Utilities to work with the singleton pattern
==============================================

[](#--------utilities-to-work-with-the-singleton-pattern----)

 [![CI badge](https://github.com/felixdorn/singleton/workflows/CI/badge.svg?branch=master)](https://github.com/felixdorn/singleton/workflows/CI/badge.svg?branch=master) [![Style CI badge](https://camo.githubusercontent.com/0e8fb51b0b5d03cfca33d1becb47f275fdceb3b3cc36fd0f49d12b0e19d0d413/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3236313537363930372f736869656c643f6272616e63683d6d6173746572267374796c653d666c6174)](https://camo.githubusercontent.com/0e8fb51b0b5d03cfca33d1becb47f275fdceb3b3cc36fd0f49d12b0e19d0d413/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3236313537363930372f736869656c643f6272616e63683d6d6173746572267374796c653d666c6174) [![Codecov](https://camo.githubusercontent.com/28f42e56a295ec47b88c557bce2a0d5184bff79af301d19695765fb87d74ab04/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f66656c6978646f726e2f73696e676c65746f6e)](https://camo.githubusercontent.com/28f42e56a295ec47b88c557bce2a0d5184bff79af301d19695765fb87d74ab04/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f66656c6978646f726e2f73696e676c65746f6e) [![License badge](https://camo.githubusercontent.com/23e12abf36d8ec17ee0e6f67c208ca618674402f998bdab23ad7d55243aea55b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f64656c69676874732f73696e676c65746f6e)](https://camo.githubusercontent.com/23e12abf36d8ec17ee0e6f67c208ca618674402f998bdab23ad7d55243aea55b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f64656c69676874732f73696e676c65746f6e) [![Last Version badge](https://camo.githubusercontent.com/08d186ebf4e82fda75dc707bdcc45f3f6bec2bf228dbda479af9e818cd6403ea/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64656c69676874732f73696e676c65746f6e)](https://camo.githubusercontent.com/08d186ebf4e82fda75dc707bdcc45f3f6bec2bf228dbda479af9e818cd6403ea/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64656c69676874732f73696e676c65746f6e)

Getting started
---------------

[](#getting-started)

### Installation

[](#installation)

This library can be installed using composer, if you don't have it already, [download it](https://getcomposer.org/download).

You can either run this command :

```
composer require delights/singleton
```

Or by adding a requirement in your `composer.json` :

```
{
  "require": {
    "delights/singleton": "1.0.0"
  }
}
```

Don't forget to run `composer install` after adding the requirement.

Disclaimer
----------

[](#disclaimer)

Most of the time, you **don't** want to use a Singleton, most of the time it's a **bad** practice. It just as bad as using global variables. But this pattern exists for a reason, it can be useful! I assume that you know what you're doing and using a singleton is the best solution for your problem.

Singleton
---------

[](#singleton)

Use it on any class you want to transform into a singleton and there you are, it's done!

Let me show you a small example.

```
use \Delight\Singleton\Singleton;

class MySingleton {
    use Singleton;
}

MySingleton::getInstance() // will always return the same instance
```

### Non Cloneable

[](#non-cloneable)

You may want to prevent cloning. There is a simple trait for that. Let me show you an example.

```
use Delight\Singleton\NotCloneable;

class SomeClass {
    use NotCloneable;
}
$instance = new SomeClass();

clone $instance // throws an NonCloneableException
```

### Not constructable

[](#not-constructable)

You may want to prevent constructing the object at all (excepted in the `getInstance` method or any object methods).

> This applies for child classes as well

```
use Delight\Singleton\NotConstructable;

class SomeOtherClass {
    use NotConstructable;
}

$someOtherClass = new SomeOtherClass(); // throws an Error, SomeOtherClass is protected
```

> You may think that this behavior is inconsistent regarding of how the cloning prevention works. However, this is the simplest way to achieve this without touching at the backtrace.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Félix Dorn](https://felixdorn.fr)

Licensing
---------

[](#licensing)

Copyright 2020 Félix Dorn

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

2250d ago

### Community

Maintainers

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

---

Top Contributors

[![felixdorn](https://avatars.githubusercontent.com/u/55788595?v=4)](https://github.com/felixdorn "felixdorn (2 commits)")

---

Tags

package

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[brysem/phpenums

Enums made simple in PHP.

10177.6k](/packages/brysem-phpenums)[o2ti/full-checkout

Full Checkout

339.9k](/packages/o2ti-full-checkout)

PHPackages © 2026

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