PHPackages                             welbertymartins/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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. welbertymartins/php-singleton

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

welbertymartins/php-singleton
=============================

Minimal and testable singleton container for object sharing and retrieval

v8.2.0(10mo ago)00MITPHPPHP ^8.2CI passing

Since Jun 29Pushed 9mo agoCompare

[ Source](https://github.com/welbertymartins/php-singleton)[ Packagist](https://packagist.org/packages/welbertymartins/php-singleton)[ RSS](/packages/welbertymartins-php-singleton/feed)WikiDiscussions main Synced 1mo ago

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

PHP Singleton Container (v.1.0.0)
=================================

[](#php-singleton-container-v100)

A minimal and testable singleton container for managing and retrieving shared instances in PHP.

---

📦 Installation
--------------

[](#-installation)

```
composer require welbertymartins/php-singleton
```

---

🚀 Quick Start
-------------

[](#-quick-start)

```
use WelbertyMartins\Singleton\Singleton;

// Store a shared object in the global singleton container
Singleton::root()->remember(fn() => new MyService());

// Retrieve it later
$service = Singleton::root()->make(MyService::class);
```

Or use an isolated container:

```
$container = Singleton::local();

$container->remember(fn() => new MyService(), 'custom_service');

$service = $container->make('custom_service');
```

---

🧪 Running Tests
---------------

[](#-running-tests)

```
composer install
composer test
```

> Includes PHPUnit tests, PSR-12 code style checking, and static analysis via Psalm.

---

📁 Project Structure
-------------------

[](#-project-structure)

```
src/     → Main singleton implementation
test/   → PHPUnit test suite

```

---

✅ Features
----------

[](#-features)

- 🧩 Global and isolated singleton instances
- 🛡️ Strict type safety
- ⚡ Lightweight, zero-dependency
- ✅ PSR-4 autoloading
- 🧪 Fully tested

---

📘 API Overview
--------------

[](#-api-overview)

### `Singleton::root(): Singleton`

[](#singletonroot-singleton)

Returns the globally shared singleton instance.

### `Singleton::local(): Singleton`

[](#singletonlocal-singleton)

Returns a new isolated singleton instance.

### `remember(callable $factory, string $name = '', bool $force = false): self`

[](#remembercallable-factory-string-name---bool-force--false-self)

Stores an object returned by a factory under a given name (or class name by default).

### `make(string $name = ''): ?object`

[](#makestring-name---object)

Retrieves a stored instance by name, or the last remembered instance.

---

📄 License
---------

[](#-license)

This project is open-sourced under the [MIT license](LICENSE).

---

🤝 Contributing
--------------

[](#-contributing)

Contributions, issues, and feature requests are welcome!

1. Fork this repository
2. Create a feature branch (`git checkout -b feature/your-feature`)
3. Submit a pull request 🚀

---

🔗 Useful Links
--------------

[](#-useful-links)

- 📦 [Packagist](https://packagist.org/packages/welbertymartins/php-singleton)

###  Health Score

30

—

LowBetter than 65% of packages

Maintenance56

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity51

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

Every ~35 days

Total

2

Last Release

279d ago

### Community

Maintainers

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

---

Top Contributors

[![welbertymartins](https://avatars.githubusercontent.com/u/38479002?v=4)](https://github.com/welbertymartins "welbertymartins (27 commits)")

---

Tags

phpcontainerdependency-injectionsingletonservice locator

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[acclimate/container

Provides adapters for various third-party service containers.

219390.6k15](/packages/acclimate-container)

PHPackages © 2026

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