Caution
This is the documentation for Pimple 3.x. If you are using Pimple 1.x, read the Pimple 1.x documentation. Reading the Pimple 1.x code is also a good way to learn more about how to create a simple Dependency Injection Container (recent versions of Pimple are more focused on performance).
Pimple is a small Dependency Injection Container for PHP.
Before using Pimple in your project, add it to your composer.json
file:
Alternatively, Pimple is also available as a PHP C extension:
Creating a container is a matter of creating a Container
instance:
As many other dependency injection containers, Pimple manages two different kind of data: services and parameters.
A service is an object that does something as part of a larger system. Examples of services: a database connection, a templating engine, or a mailer. Almost any global object can be a service.
Services are defined by anonymous functions that return an instance of an object:
Notice that the anonymous function has access to the current container instance, allowing references to other services or parameters.
As objects are only created when you get them, the order of the definitions does not matter.
Using the defined services is also very easy:
By default, each time you get a service, Pimple returns the same instance of it. If you want a different instance to be returned for all calls, wrap your anonymous function with the factory()
method
Now, each call to $container['session']
returns a new instance of the session.
Defining a parameter allows to ease the configuration of your container from the outside and to store global values:
If you change the session_storage
service definition like below:
You can now easily change the cookie name by overriding the session_storage_class
parameter instead of redefining the service definition.
Because Pimple sees anonymous functions as service definitions, you need to wrap anonymous functions with the protect()
method to store them as parameters:
In some cases you may want to modify a service definition after it has been defined. You can use the extend()
method to define additional code to be run on your service just after it is created:
The first argument is the name of the service to extend, the second a function that gets access to the object instance and the container.
If you use the same libraries over and over, you might want to reuse some services from one project to the next one; package your services into a provider by implementing Pimple\ServiceProviderInterface
:
Then, register the provider on a Container:
When you access an object, Pimple automatically calls the anonymous function that you defined, which creates the service object for you. If you want to get raw access to this function, you can use the raw()
method: