Wallogit.com
2017 © Pedro Peláez
Heroine has come to rescue you from your DI nightmares by providing a simple, dependency-free service locator and repository., (*2)
For more information on the service locator pattern, you can read the Wikipedia article or on Martin Fowler's website., (*3)
How and where you configure Heroine is up to you. All you need to do is construct
a new Heroine\Heroine object with an array or an instance of Heroine\Config., (*4)
A sample array configuration follows:, (*5)
array(
'MyClass' => 'MyClass',
'another_class' => 'Another\Class',
),
'callables' => array(
'MyCalledClass' => function(Heroine\Heroine $heroine) {
$instance = new MyCalledClass;
$instance->setMyClass($heroine->get('MyClass'));
return $instance;
},
'another_called_class' => function(Heroine\Heroine $heroine) {
$instance = new Another\Called\Class;
$instance->setAnotherClass($heroine->get('another_class'));
return $instance;
},
),
'factories' => array(
'MyFactoryProductClass' => 'MyFactoryClass',
),
'initializers' => array(
function (Heroine\Heroine $heroine, $instance) {
if ($instance instanceof Heroine\HeroineAwareInterface)
{
$instance->setHeroine($heroine);
}
},
function (Heroine\Heroine $heroine, $instance) {
if ($instance instanceof InitializableInterface)
{
$instance->initalize();
}
},
),
);
?>
The four top-level array keys here represent the four creation patterns that Heroine provides., (*6)
createService method will be called to fetch an instance. This is recommended for objects with many dependencies or complex configuration.