Pimple keeps services and parameters in one array, and protect() is how you tell them apart
A small PHP dependency injection container
At a glance
- What is it?
- Pimple is a small MIT-licensed PHP dependency injection container that has been closed to changes: no new features, no cosmetic patches, only compatibility with newer PHP versions and security fixes. Its default branch was last pushed on 2026-03-02 and it has no GitHub releases.
- Who is it for?
- Pimple suits a small PHP codebase that wants services and parameters in one array and can accept that the array decides which is which. It does not suit code that needs the container itself to be PSR-11, or a documented list of supported PHP versions, or new features.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Activity is slowing. The repository last received commits 8 months ago.
- What is it written in?
- Mainly PHP, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 15, 2026, and from our analysis. They are not legal advice.
Star history
- silexphp/Pimple2,661 ★ · -1
| Repository | 2026-10-04 | 2026-11-15 |
|---|---|---|
| silexphp/Pimple | 2.7k | 2.7k |
-1 Stars in the last 42 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
The container is an array where a closure means build me an object
There is no configuration file and no builder. A container is an instance of `Pimple\Container` that behaves like an array, and what you put in a slot decides what happens when you read it. A closure in a slot is a service definition; anything else is a parameter.
use Pimple\Container;
$container = new Container();// define some services
$container['session_storage'] = fn($c) => new SessionStorage('SESSION_ID');
$container['session'] = fn($c) => new Session($c['session_storage']);
// get the session object
$session = $container['session'];
// the above call is roughly equivalent to the following code:
// $storage = new SessionStorage('SESSION_ID');
// $session = new Session($storage);Two properties fall out of that arrangement. The closure receives the container as its argument, so a definition can reach other services and parameters by name instead of taking them as constructor arguments. And because nothing is built until a slot is read, the order of definitions does not matter: `session` may be written above `session_storage` and still resolve, because the read happens later. Each read returns the same instance, which is why the commented equivalent above builds one storage and one session rather than two of each.
protect() exists because a closure in the array would be called
Reading the array is also what makes `protect()` necessary. If every closure were treated as something to invoke, storing a closure as a plain value, say a random number generator to be handed to some other service, would run it at read time instead of giving you the function.
$container['random_func'] = $container->protect(fn() => rand());`protect()` marks the closure as data. Reading that slot returns the function itself rather than its result, which is what lets a callback travel through the container like any other parameter. The cost is that the kind of a slot is decided by how it was written rather than declared anywhere: there is one map, and the wrapper is the only thing separating a value from a definition.
That is also why parameters are framed as configuration that should be adjustable from outside, a cookie name or a class name. Overriding `$container['cookie_name']` changes what the storage service builds without rewriting the storage definition, because that definition reads the parameter through the container it was handed.
factory() is the only opt out from the shared instance
Sharing is the default and the exception is explicit. Every read of a service slot calls the definition once and keeps the result, so reading `$container['session']` twice yields one session. When that is the wrong answer, the definition gets wrapped in `factory()`:
$container['session'] = $container->factory(fn($c) => new Session($c['session_storage']));Each read now builds a new session and the memoized value is not kept. There is nothing between those two behaviours: a service is either cached for the life of the container or constructed on demand, and no part of the container records which of the two a given slot uses. That choice also decides what happens to state inside the object. A shared instance keeps whatever it accumulates for as long as the container lives, which is the point for a service such as a database connection or a mailer, and the wrong shape for a value that has to start clean each time it is read. For a container that mixes request-scoped and process-scoped objects in one map, that is the detail worth remembering, because at the point of definition the two look identical.
extend() runs right after creation and has to hand the object back
`extend()` is the one place where behaviour is added to a service after it already exists. The first argument names the service, the second is a function that receives the created object together with the container, and it runs immediately after construction:
$container['session_storage'] = fn($c) => new $c['session_storage_class']($c['cookie_name']);
$container->extend('session_storage', function ($storage, $c) {
$storage->...();
return $storage;
});The return value is not optional. The function has to hand the object back, which is what lets several extensions stack on the same service in sequence, each one seeing the previous one's result. The documented body is itself a placeholder, a call written as `$storage->...()`, so the shape of a real extension has to come from the class you are extending rather than from that example.
Providers move services between projects, raw() gets you the definition
Services that keep recurring across projects are meant to be packaged. A provider is a class implementing `Pimple\ServiceProviderInterface` with a `register(Container $pimple)` method that writes services and parameters onto the container it is handed:
use Pimple\Container;
class FooProvider implements Pimple\ServiceProviderInterface
{
public function register(Container $pimple)
{
// register some services and parameters
// on $pimple
}
}Attaching one is a single call on the container:
$pimple->register(new FooProvider());The same split between definition and instance reappears in `raw()`. Reading a service slot normally invokes the closure for you, while `raw()` returns the closure itself:
$container['session'] = fn($c) => new Session($c['session_storage']);
$sessionFunction = $container->raw('session');The object is therefore not built when the slot is read, and the definition stays reachable after it has been used. That is the way to inspect or wrap a definition rather than consume the object it builds.
The container is not PSR-11, for historical reasons
`Container` does not implement `Psr\Container\ContainerInterface`. The stated reason is historical, which is an unusual admission for a dependency injection library to make, and the workaround is a wrapper class:
use Pimple\Container;
use Pimple\Psr11\Container as PsrContainer;
$container = new Container();
$container['service'] = fn($c) => new Service();
$psr11 = new PsrContainer($container);
$controller = function (PsrContainer $container) {
$service = $container->get('service');
};
$controller($psr11);The wrapper is what decouples consuming code from the Pimple class: the controller closure is typed against the PSR-11 interface and never names Pimple, so it would take any PSR-11 container. What you give up by going through it is the array syntax and the Pimple-only helpers, since the wrapper exposes `get()` and the rest of the interface instead of the underlying offsets. The example wraps an ordinary Pimple container rather than constructing the PSR-11 one from scratch, so both objects exist at once in the same process.
ServiceLocator exists so services stop receiving the whole container
Some services need several others without knowing which ones they will actually touch. The traditional answer, injecting the entire container, is discouraged here for two stated reasons: it gives services broader access to the application than they need, and it hides their real dependencies. `ServiceLocator` is the alternative, offering a fixed set of services and constructing each one only when it is read.
It also allows a service to be published under a name other than the one it was registered with, which is how an object expecting a particular interface gets handed the right thing. Access is bounded by that published set rather than by every slot in the container, and that bound is the mechanism behind both of the objections listed against injecting the container itself. The paragraph making the point about naming stops in the middle of the name it meant to use as an example, after `EventDispat`, with nothing following it, so the full class name and the call itself are not spelled out. The section introducing the locator sits under the PSR-11 heading, which is where its namespace belongs, but the constructor shape has to be read from the source rather than from the prose.
Closed for changes, no releases, and nothing in the tree lists PHP versions
Two caution blocks sit above everything else. Pimple is closed for changes: no new features, no cosmetic changes, and the only accepted changes are compatibility with newer PHP versions and security issue fixes. The second block says the documentation here covers 3.x, points 1.x users to separate 1.x documentation, and adds that reading the 1.x code is a good way to learn how a simple container is put together, since recent versions are more focused on performance.
The repository state agrees with that notice. There are no GitHub releases, and the default branch was last pushed on 2026-03-02. Installing takes one Composer line:
$ ./composer.phar require pimple/pimple "^3.0"The tree itself is small and mostly tooling: `src/` for the code, `README.rst` written in reStructuredText rather than Markdown, `composer.json`, `phpunit.xml.dist`, a `.php_cs.dist` style configuration, a `CHANGELOG` file with no extension, `LICENSE`, and `.github/` with `.gitignore` beside it. There is no `docs/` directory and no version matrix, so which PHP releases are supported is stated only as a promise of compatibility fixes, never as a list.
Editorial conclusion
Pimple suits a small PHP codebase that wants services and parameters in one array and can accept that the array decides which is which. It does not suit code that needs the container itself to be PSR-11, or a documented list of supported PHP versions, or new features. Before adopting it, check three things: which PHP versions the compatibility fixes have actually covered, whether your services are safe to share as a single instance or need `factory()`, and how much weight your project will rest on a container its own authors have closed to changes. The 1.x branch is pointed at as the readable version, which says plainly where the design went.
Frequently asked questions
What is Pimple used for?
Pimple is a small dependency injection container for PHP under the MIT license. It holds two kinds of data in one array, services built from closures and plain parameters, and it is closed for changes, so only compatibility with newer PHP versions and security fixes are accepted.
How do I install Pimple?
The documented line is ./composer.phar require pimple/pimple with a caret and point zero constraint, which targets the 3.x line. Users of 1.x are sent to separate 1.x documentation, and the readme notes that reading the 1.x code is a good way to learn how a simple container is built.
Does the Pimple container implement PSR-11?
No. For historical reasons the Container class does not implement Psr\Container\ContainerInterface. Pimple\Psr11\Container wraps an underlying Pimple container and exposes its content through the PSR-11 methods.
How do I get a new instance from Pimple on every read?
Wrap the service definition in the factory() method. By default each read of a service returns the same instance, and a factory definition constructs a new one every time it is read.
Is Pimple still being worked on?
Its own caution block says it is closed for changes: no new features, no cosmetic changes, only PHP compatibility work and security fixes. The repository has no GitHub releases and the default branch was last pushed on 2026-03-02.
What is the Pimple ServiceLocator for?
It gives a service access to a set of predefined services while instantiating them only when they are needed, instead of injecting the whole container, which is described as giving services too broad access and hiding their actual dependencies. It can also expose a service under a different name than the one it was registered with.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/silexphp-pimple)Star history chart for your README
[](https://hysenlabs.com/projects/silexphp-pimple)Was this useful?