PHPackages                             antares/pool - 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. antares/pool

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

antares/pool
============

A simple PHP pool.

v1.2(10y ago)026MITPHPPHP &gt;=5.4.0

Since Dec 22Pushed 10y ago2 watchersCompare

[ Source](https://github.com/antares993/Pool)[ Packagist](https://packagist.org/packages/antares/pool)[ Docs](https://github.com/antares993/Pool)[ RSS](/packages/antares-pool/feed)WikiDiscussions master Synced 3w ago

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

Pool
====

[](#pool)

[![SensioLabsInsight](https://camo.githubusercontent.com/9175215c573181e410d08fa8a7ce9840f737f5ccf221abc99c367bfdfcf19b52/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f37376431343037372d613535632d343238322d386632372d6338343735643162663764612f6d696e692e706e67)](https://insight.sensiolabs.com/projects/77d14077-a55c-4282-8f27-c8475d1bf7da)[![Build Status](https://camo.githubusercontent.com/f25f7072e9209f4ed12bef0529910b7567309aa77b28e7b91a114e468481c50e/68747470733a2f2f7472617669732d63692e6f72672f616e74617265733939332f506f6f6c2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/antares993/Pool)[![Latest Stable Version](https://camo.githubusercontent.com/77cce5de63f08f91febce8f552fd8e72cd42dedba363dff04ea6d5cb77804eb0/68747470733a2f2f706f7365722e707567782e6f72672f616e74617265732f706f6f6c2f762f737461626c65)](https://packagist.org/packages/antares/pool)[![License](https://camo.githubusercontent.com/c223ee8a282a7ba3a517f4d09a982382f232d1a3a682c9bfb5b0490075108b1c/68747470733a2f2f706f7365722e707567782e6f72672f616e74617265732f706f6f6c2f6c6963656e7365)](https://packagist.org/packages/antares/pool)

This library provides you simple pools. This is especially useful when working with [React](http://reactphp.org/): this way you may share resources such as database connections and boost your application's performance.

**Suggestions and contributions are welcome!**

Install
-------

[](#install)

You can add this library as a dependency using composer this way:

```
composer require antares/pool

```

Compatibility
-------------

[](#compatibility)

This library is compatible with PHP 5.4+, PHP 7 and HHVM.

Documentation
-------------

[](#documentation)

### Resources accepted by the pools

[](#resources-accepted-by-the-pools)

First of all, please note that the pools provided only store objects. Values such as integers, string or arrays cannot be stored. If needed, you can wrap these values into a `stdClass`.

### `PoolInterface`

[](#poolinterface)

The pools provided implement the `PoolInterface` interface, which provides the following methods:

```
interface PoolInterface
{
    // Associate a resource to an id
    public function set($id, $generator, $eventsCallbacks = null);

    // Get an instance of the resource
    public function get($id);

    // Free the instance of the resource
    public function dispose($instance);

    // Clear the pool
    public function clear();

    // Set a set of callbacks for the events a resource can trigger
    public function addEventsCallbacks($id, $callbacks);

    // Set a callback to call when an event is triggered for given resource
    public function addEventCallback($id, $event, $callback);
}
```

### `Pool`

[](#pool-1)

The most basic pool provided is the `Pool` class. Let's see an example of how it can be used.

```
use Pool\Pool;

class Foo {}

$pool = new Pool(); // Instantiate the pool
$pool->set('foo', function() { return new Foo(); }); // Assign to the id 'foo' a generator returning an instance of Foo

// If an instance is already available, the pool will return it
// Else, a new instance will be created
$foo = $pool->get('foo');

// We don't need $foo anymore, so the instance can be released
// It will be available from the pool again
$pool->dispose($foo);
```

As shown in this example, it is important to release the resource when it is not needed anymore. If you don't, a new instance will be created each time the resource will be asked, and will stay in the memory.

### `StaticPool`

[](#staticpool)

Given the structure of your application, the code where you define your pool may be uselessly executed several times. In this case, the `StaticPool` can be useful. Internally, it contains a static instance of `Pool` which is shared between every instance of `StaticPool`. Take a look at the following piece of code:

```
use Pool\StaticPool;

class Foo {}

$pool = new StaticPool();

if (!$pool->isAlreadyDefined()) {
    $pool->set('foo', function() { return new Foo(); });
}

$foo = $pool->get('foo');
$pool->dispose($foo);
```

Here, the first time the pool is instanciated, its resources will be defined. The following times, the definition step will be skipped. Each instance of `StaticPool` will use the same internal pool and its use is eased.

### Pool events

[](#pool-events)

You can attach callbacks that will be called when some events are triggered by the pool.

4 events may happen:

- `PoolInterface::EVENT_GET` when the instance of a resource is pulled from the pool (when calling `$pool->get($id)`)
- `PoolInterface::EVENT_DISPOSE` when the instance is released (when calling `$pool->dispose($instance)`)
- `PoolInterface::EVENT_CREATE` when the instance is created (when calling `$pool->get($id)` if the instance has not been already created)
- `PoolInterface::EVENT_DESTRUCT` when the instance is destructed (when calling `$pool->clear()`)

There are 3 ways to define callbacks:

```
use Pool\PoolInterface;

$pool = new Pool\Pool();

$callbacks = [
    PoolInterface::EVENT_GET => [function($instance) {}],
    PoolInterface::EVENT_DISPOSE => [function($instance) {}]
];

// 1. Define it with the generator
$pool->set('foo', function() { return new Foo(); }, $callbacks);

// 2. Define each event
$pool->addEventCallback('foo', PoolInterface::EVENT_GET, $callbacks[PoolInterface::EVENT_GET][0]);
$pool->addEventCallback('foo', PoolInterface::EVENT_DISPOSE, $callbacks[PoolInterface::EVENT_DISPOSE][0]);

// 3. Define all callbacks in one method call
$pool->addEventsCallbacks('foo', $callback);
```

Note that the callbacks are combinable.

```
$pool->addEventCallback('foo', PoolInterface::EVENT_GET, function($instance) { echo 'a'; });
$pool->addEventCallback('foo', PoolInterface::EVENT_GET, function($instance) { echo 'b'; });

$pool->get('foo'); // will echo 'a' and 'b'
```

Below is an example of how you can use callbacks.

```
class Foo {
    public function bar() { echo 'blablabla'; }
}

$counter = 0;
$pool = new Pool\Pool();
$pool->set('foo', function() { return new Foo(); }, [
    PoolInterface::EVENT_GET => [function($instance) { $instance->bar(); }],
    PoolInterface::EVENT_DISPOSE => [function($instance) use (&$counter) { $counter++; }]
]);

$foo = $pool->get('foo'); // $foo->bar() is called

$pool->dispose($foo); // the counter is incremented
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 94.1% 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 ~0 days

Total

3

Last Release

3836d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/55869ec69e18049202985f8d3581e805059bb843cef71f5a8eed3d1d7592ab0e?d=identicon)[antares993](/maintainers/antares993)

---

Top Contributors

[![antarestupin](https://avatars.githubusercontent.com/u/7022740?v=4)](https://github.com/antarestupin "antarestupin (16 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

pool

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/antares-pool/health.svg)

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

###  Alternatives

[lifo/php-ipc

Simple PHP Inter Process Communication (IPC) library

285.6k](/packages/lifo-php-ipc)

PHPackages © 2026

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