Why use a closure to assign instead of directly assigning a value to a key?

I watched this video , and at 7:10 he added a db dependency and uses closure to assign a value.

My question is, why not just use direct assignment instead, I mean that I am not doing this:

$container['db'] = $capsule;

equivalent to this:

$container['db'] = function ($container) use ($capsule) {
    return $capsule;
}

If not, what's the difference and which way is better?

+6
source share
3 answers

TL; DR , because defining dependencies as closures allows the dependency injection container to create them on demand, so you donโ€™t have to worry about how to define them and manually manage their dependencies.

Pimple , , .

, Pimple , , :

$container['sample-param'] = 'foo';
echo $container['sample-param'];
//output: foo

, sample-param - , , . :

$container['myService'] = function($c) {
    $service = new \Some\Complicated\Service();
    //this service depends on cache service
    $service->setCache($c['cache']);
    return $service;
}

$container['cache'] = function($c) {
    $cache = new \Cache\Driver();
    //our cache driver needs a db connection
    $cache->setDbConnection($c['db']);
    return $cache;
}

$container['db'] = function($c) {
    //our database connection requires some parameters
    $dbConnection = new \Database\Connection($c['db-config']);
    return $dbConnection;
}

$container['db-config'] = [
    'username' => 'foo',
    'password' => 'bar',
    'host' => 'localhost'
];

//Now we want to user our service:
$container['myService']->doSomething();

, .

myService cache, myService. Pimple , , Pimple . myService, Pimple , myService , , . , Pimple ($ c - Pimple) dependecies ( cache). Pimple , , ..... , , , db-config, . .

, , ? , myService , . , , , , . myService , cache, .

+2

Slim framework , Pimple .

http://pimple.sensiolabs.org/

- , - . : , . .

, :

// define some services
$container['session_storage'] = function ($container) {
    return new SessionStorage('SESSION_ID');
};

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

, , .

Pimple \ArrayAccess (. ), , , . offsetSet offsetGet, , :

$container['a'] = function($c){ return new Blah();}

$thing = $container['a']; 

tl; dr Pimple, PHP.

0

This is completely different. The first $container['db'] = $capsule;
means that the array $containerhas a key db, and this key is equal $capsule, the second means that you have an array $container, and the key dbhas a close value. You can see the difference when resetting two results:

Closing example:

$capsule = 1;
$container['db'] = function ($container) use ($capsule) {
    return $capsule;
};
var_dump($container); 

Result:

Array(1) {
  ["db"]=>
  object(Closure)#1 (2) {
    ["static"]=>
    array(1) {
      ["capsule"]=>
      int(1)
    }
    ["parameter"]=>
    array(1) {
      ["$container"]=>
      string(10) "<required>"
    }
  }
}

Example without closing:

$capsule = 1;
$container['db'] = $capsule;    
var_dump($container);

Result:

result array(1) {
  ["db"]=>
  int(1)
}

You can read about closure in the php manual for a better understanding of their role. PHP closing guide

-one
source

Source: https://habr.com/ru/post/1016785/


All Articles