Created
August 21, 2018 22:37
-
-
Save brentk/51f58fafeee8029d7e8b1e838eca3d5b to your computer and use it in GitHub Desktop.
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
| require(__DIR__ . "/vendor/autoload.php"); | |
| interface IDog { | |
| function bark(); | |
| } | |
| class Poodle implements IDog { | |
| public function bark() { | |
| echo "woof!" . PHP_EOL; | |
| } | |
| } | |
| class Kennel { | |
| protected $dog; | |
| public function __construct(IDog $dog) { | |
| $this->dog = $dog; | |
| } | |
| public function pokeDog() { | |
| $this->dog->bark(); | |
| } | |
| } | |
| $containerBuilder = new \DI\ContainerBuilder(); | |
| $containerBuilder->addDefinitions([ | |
| "IDog" => \DI\autowire("Poodle") | |
| ]); | |
| $container = $containerBuilder->build(); | |
| //Works: | |
| $mydog = $container->get("IDog"); | |
| $mydog->bark(); | |
| //Does not work: | |
| $kennel = $container->get("Kennel"); | |
| $kennel->pokeDog(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment