Skip to content

Instantly share code, notes, and snippets.

@juzna
Created April 19, 2012 19:48
Show Gist options
  • Save juzna/2423666 to your computer and use it in GitHub Desktop.
Save juzna/2423666 to your computer and use it in GitHub Desktop.
Nette DI container and @Inject annotation
<?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!)
}
<?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