A simple PHPDependency Injection Container

Pimple is a simple PHP Dependency Injection Container

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).

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:

1
2
3
4
5
6

// 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);

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:

1
2
3
4
5
6
7
8
9
10

usePimple\Container;classFooProviderimplementsPimple\ServiceProviderInterface{publicfunctionregister(Container$pimple){// register some services and parameters// on $pimple}}

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: