Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A small PHP 5.3 dependency injection container
PHP C
tree: 6213e14d6f

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
tests
.gitignore
LICENSE
README.rst
composer.json
phpunit.xml.dist

README.rst

Pimple

Pimple is a small Dependency Injection Container for PHP 5.3 that consists of just one file and one class (about 50 lines of code).

Download it, require it in your code, and you're good to go:

require_once '/path/to/Pimple.php';

Creating a container is a matter of instating the Pimple class:

$container = new Pimple();

As many other dependency injection containers, Pimple is able to manage two different kind of data: objects and parameters.

Defining Parameters

Defining a parameter is as simple as using the Pimple instance as an array:

// define some parameters
$container['cookie_name'] = 'SESSION_ID';
$container['session_storage_class'] = 'SessionStorage';

Defining Objects

Objects are defined by anonymous functions that return an instance of the object:

// define some objects
$container['session_storage'] = function ($c) {
    return new $c['session_storage_class']($c['cookie_name']);
};

$container['session'] = function ($c) {
    return new Session($c['session_storage']);
};

Notice that the anonymous function has access to the current container instance, allowing references to other objects or parameters.

As objects are only created when you get them, the order of the definitions does not matter, and there is no performance penalty.

Using the defined objects is also very easy:

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

Defining Shared Objects

By default, each time you get an object, Pimple returns a new instance of it. If you want the same instance to be returned for all calls, wrap your anonymous function with the share() method:

$c['session'] = $c->share(function ($c) {
    return new Session($c['session_storage']);
});

Protecting Parameters

As Pimple makes no difference between a parameter and an object, you can use the protect() method if you need to define a parameter as an anonymous function:

$c['random'] = $c->protect(function () { return rand(); });

Modifying Objects after creation

In some cases you may want to modify an object after it has been created. You can use the extend() method to do just that:

$c['twig'] = $c->share(function ($c) {
    return new Twig_Environment($c['twig.loader'], $c['twig.options']);
});

$c['twig'] = $c->extend(function ($twig, $c) {
    $twig->addExtension(new MyTwigExtension());
    return $twig;
});

Fetching the Object creation function

When you access an Object, Pimple automatically calls the function that you defined, which creates the Object for you. If you want to get this function, you can use the raw() method:

$c['session'] = $c->share(function ($c) {
    return new Session($c['session_storage']);
});

$sessionFunction = $c->raw('session');

Packaging a Container for reusability

If you use the same libraries over and over, you might want to create reusable containers. Creating a reusable container is as simple as creating a class that extends Pimple, and configuring it in the constructor:

class SomeContainer extends Pimple
{
    public function __construct()
    {
        $this['parameter'] = 'foo';
        $this['object'] = function () { return stdClass(); };
    }
}

Using this container from your own is as easy as it can get:

$container = new Pimple();

// define your project parameters and objects
// ...

// embed the SomeContainer container
$container['embedded'] = $container->share(function () { return new SomeContainer(); });

// configure it
$container['embedded']['parameter'] = 'bar';

// use it
$container['embedded']['object']->...;
Something went wrong with that request. Please try again.