Skip to content

Instantly share code, notes, and snippets.

@brentk
Created August 21, 2018 22:37
Show Gist options
  • Select an option

  • Save brentk/51f58fafeee8029d7e8b1e838eca3d5b to your computer and use it in GitHub Desktop.

Select an option

Save brentk/51f58fafeee8029d7e8b1e838eca3d5b to your computer and use it in GitHub Desktop.
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