Created
April 19, 2012 19:48
-
-
Save juzna/2423666 to your computer and use it in GitHub Desktop.
Nette DI container and @Inject annotation
This file contains hidden or 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 | |
/** | |
* Example of what a decent Dependency Injection Container should inject to make our life happier | |
*/ | |
class MyTest { | |
public function __construct(\Nette\Security\User $user) { // Constructor injection | |
} | |
public function setUser1(\Nette\Security\User $user) { // Setter injection, must be explicitly defined (not automagic by DIC) | |
} | |
/** @inject */ | |
public function setUser2(\Nette\Security\User $user) { // Setter injection with automagic by DIC, no need for more config | |
} | |
/** @inject("pepa", "novak") */ | |
public function setUser2($firstname, $surname, \Nette\Security\User $user) { // automagic and more parameters | |
} | |
/** | |
* @var \Nette\Security\User | |
* @inject | |
*/ | |
public $x; // property injection | |
/** | |
* @var \Nette\Security\User | |
* @inject("@mySpecialUser") | |
*/ | |
private $y; // also for protected and private members (but not very nice!) | |
} |
This file contains hidden or 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 | |
/** | |
* DI Container can also help you injecting into objects created from elsewhere | |
*/ | |
$test = new MyTest($user); // constructor injection mandatory | |
$container->inject($test); // injects by @inject annnot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment