Skip to content

Instantly share code, notes, and snippets.

@willemwollebrants
Created July 7, 2016 23:41
Show Gist options
  • Save willemwollebrants/87ad60d2ed5579c5c70f022fa005f88b to your computer and use it in GitHub Desktop.
Save willemwollebrants/87ad60d2ed5579c5c70f022fa005f88b to your computer and use it in GitHub Desktop.
<?php
$examples = new League\Container\Container();
class NonInvokableExample
{
}
class InvokableExample
{
public function __invoke()
{
return 'a value';
}
}
$examples->add("InvokableAsClassname", InvokableExample::class);
$examples->add("InvokableAsClosure", function() {
return new InvokableExample();
});
$examples->add("InvokableAsInstance", new InvokableExample());
var_dump($examples->get("InvokableAsClassname"));
//returns object(InvokableExample)
var_dump($examples->get("InvokableAsClosure"));
//returns object(InvokableExample)
var_dump($examples->get("InvokableAsInstance"));
//returns string 'a value'
$examples->add("NonInvokableAsClassname", NonInvokableExample::class);
$examples->add("NonInvokableAsClosure", function() {
return new NonInvokableExample();
});
$examples->add("NonInvokableAsInstance", new NonInvokableExample());
var_dump($examples->get("NonInvokableAsClassname"));
//returns object(NonInvokableExample)
var_dump($examples->get("NonInvokableAsClosure"));
//returns object(NonInvokableExample)
var_dump($examples->get("NonInvokableAsInstance"));
//returns object(NonInvokableExample)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment