Created
January 18, 2017 04:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"aura/di": "^3.2" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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