Skip to content

Instantly share code, notes, and snippets.

@harikt
Created January 18, 2017 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harikt/77b590530110bec303e1d8f9598b453b to your computer and use it in GitHub Desktop.
Save harikt/77b590530110bec303e1d8f9598b453b to your computer and use it in GitHub Desktop.
Discussion : Aura DI lazy function called each time https://groups.google.com/d/msg/auraphp/Ndh4pHM3m_k/PZdhOXtBAgAJ
{
"require": {
"aura/di": "^3.2"
}
}
<?php
require __DIR__ . '/vendor/autoload.php';
class Hello
{
public $database;
public function __construct()
{
$this->database = 'yes';
}
}
class World
{
public function __construct()
{
}
public function process()
{
var_dump("What ?");
}
}
use Aura\Di\ContainerBuilder;
$builder = new ContainerBuilder();
$di = $builder->newInstance();
$di->set('config', $di->lazyNew(Hello::class));
$di->set('pdo', $di->lazy(function () use ($di) {
echo "Called once ?";
$config = $di->get('config');
if($config->database){
return $di->lazyNew(World::class)();
} else {
return null;
}
}));
$di->get('pdo'); // configuration is checked
$di->get('pdo'); // configuration is checked again :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment