Last active
December 21, 2015 18:09
-
-
Save lisachenko/6345683 to your computer and use it in GitHub Desktop.
Autowiring preview
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 | |
use Warlock\Annotation\Autowired; | |
class Example | |
{ | |
/** | |
* @Autowired("logger", required=true) | |
* @var LoggerInterface | |
*/ | |
protected $logger = null; // no public properties for property injection, only protected services | |
// no constructor injection | |
// no setter injection | |
public function test() | |
{ | |
$this->logger->info("Logger injected only here due to request to the this->logger"); | |
echo 'Yay'; | |
} | |
} |
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 | |
// somewhere at the kernel ... | |
$container = new DiContainer(); | |
// our code, this can be plain entities (POPO) that not defined in the container | |
$instance = new Example(); // No services injection at all! | |
$instance->test(); // logger will be requested and injected from the container automatically by Warlock |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Huh, didn't think about injection into entities. Interesting.
Usually this is a bad practice, but sometimes this could be really useful to make the code transparent and easy.