fork download
  1. <?php
  2.  
  3. class SomeComponent
  4. {
  5. public function doSomething()
  6. {
  7. }
  8. }
  9.  
  10. class App
  11. {
  12. public function __construct(SomeComponent $theComponent)
  13. {
  14. $this->theComponent = $theComponent;
  15. }
  16.  
  17. public function run()
  18. {
  19. $this->theComponent->doSomething();
  20. }
  21. }
  22.  
  23. class DIContainer{
  24. public function get($className) {
  25. switch ($className) {
  26. case SomeComponent::class:
  27. return new SomeComponent;
  28. case App::class:
  29. return new App($this->get(SomeComponent::class));
  30. default:
  31. throw new Exception;
  32. }
  33. }
  34. }
  35.  
  36. $DIContainer = new DIContainer();
  37. $app = $DIContainer->get(App::class);
  38. $app->run();
Success #stdin #stdout 0.01s 24272KB
stdin
Standard input is empty
stdout
Standard output is empty