Download the PHP package antares/pool without Composer
On this page you can find all versions of the php package antares/pool. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package pool
Short Description A simple PHP pool.
License MIT
Homepage https://github.com/antares993/Pool
Informations about the package pool
Pool
This library provides you simple pools. This is especially useful when working with React: this way you may share resources such as database connections and boost your application's performance.
Suggestions and contributions are welcome!
Install
You can add this library as a dependency using composer this way:
Compatibility
This library is compatible with PHP 5.4+, PHP 7 and HHVM.
Documentation
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
The pools provided implement the PoolInterface
interface, which provides the following methods:
Pool
The most basic pool provided is the Pool
class. Let's see an example of how it can be used.
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
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:
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
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:
Note that the callbacks are combinable.
Below is an example of how you can use callbacks.