Tuesday, August 18, 2015

Pimple dependency injector container. Lambdas and Closures

As you probably saw in a previous post a give it a try on Laravel and I felt is too high level at this moment and I should learn more about the magic behind scene not just how to use a certain framework.
So I looked for a microframework hoping to be easier to understand and I discovered Silex  from the guy who made Symfony: "Silex is a PHP microframework for PHP. It is built on the shoulders of Symfony2 and Pimple and also inspired by sinatra." Next question what is Pimple?
Answer: Pimple is a small Dependency Injection Container for PHP. ( http://pimple.sensiolabs.org/ )

I have no idea about Dependency Injectors, but  found some good materials about Dependency Injection and Pimple here:

http://www.sitepoint.com/dependency-injection-with-pimple

http://fabien.potencier.org/do-you-need-a-dependency-injection-container.html

http://www.slideshare.net/fabpot/dependency-injection-in-php-5354 


Some key ideas from the Fabien's slides:


A Dependency Injector Container/ Service Container manages your SERVICES
Good rule of thumb: It manages "global" objects (objects with only one instance !=Singleton)
Example of such objects: a User, a Request, a database Connetcion, a Logger
Unlike Model objects (a Product, a blog Post)

Dependency Injection Container is simply a PHP object that manages instantiation of other objects. we call them services.


Before starting to read these articles I have to tell you that the actual implementation is based on lambdas and closures. You should have a look before on the following links to get the idea behind this PHP features:

http://culttt.com/2013/03/25/what-are-php-lambdas-and-closures/


About closure binding here:  https://www.christophh.net/2011/10/26/closure-object-binding-in-php-54/
From this article http://programmingarehard.com/2013/10/16/we-need-some-closure.html/ :

"Nowadays, it's very common to see Closures being used for containers. These containers' purpose is to create objects and inject their dependencies so we don't have to do that every single time. Consider the following example.

$container = [];

$container['EmailTemplate'] = function(){

    return new EmailTemplate;
};

$container['DataSource'] = function(){

    return new MySQLDataSource;
};

$container['NewsLetterSender'] = function() use ($container) {

    //used to created emails
    $template   = $container['EmailTemplate']();

    //used to track stats about the sending
    $dataSource = $container['DataSource']();

    return new NewsLetterSender($dataSource, $template);
};

$newsLetterSender = $container['NewsLetterSender']();

//versus the more verbose and less flexible

$newsLetterSender = new NewsLetterSender(new EmailTemplate, new MySQLDataSource);
 
As you can see, the $container's only responsibility is to create and assemble new objects. This concept is known as Inversion of Control. It's so popular because it's an elegant way to encourage dependency injection."




No comments:

Post a Comment