fork download
  1. <?php
  2.  
  3. class DepInj{
  4. public $declarations = array();
  5.  
  6. function declare($name, $class, $args=array()){
  7. $this->declarations[$name] = array(
  8. 'class' => $class,
  9. 'args'=> $args
  10. );
  11. }
  12.  
  13. function getObj($name){
  14. $decl = $this->declarations[$name];
  15.  
  16. $argsObjs = array();
  17. foreach($decl['args'] as $a_kay=>$arg_class_name){
  18. $argsObjs[] = $this->getObj($arg_class_name);
  19. }
  20.  
  21. $r = new ReflectionClass($decl['class']);
  22. $myInstance = $r->newInstanceArgs(argsObjs);
  23. return $myInstance;
  24. }
  25.  
  26.  
  27. }
  28.  
  29. class CheapGears{
  30. public $price = 5;
  31. }
  32.  
  33. class ExpensiveGears{
  34. public $price = 20;
  35. }
  36.  
  37. class Engine{
  38. public $hp=200;
  39. public $gears=null;
  40. function __construct($gears){
  41. $this->gears = $gears;
  42. }
  43. }
  44.  
  45. class TurboEngine{
  46. public $hp=1200;
  47. public $gears=null;
  48. function __construct($gears){
  49. $this->gears = $gears;
  50. }
  51. }
  52.  
  53. class Car{
  54. public $engine = null;
  55. function __construct($engine){
  56. $this->engine = $engine;
  57. }
  58. }
  59.  
  60. $container = new DepInj();
  61. $container->declare('normalGear',CheapGears);
  62. $container->declare('skupGear',ExpensiveGears);
  63.  
  64. $container->declare('normalGearNormalEngine','Engine', array('normalGear') );
  65. $container->declare('skupGearNormalEngine','Engine', array('skupGear') );
  66.  
  67. $container->declare('skupEngine','TurboEngine', array('skupGear'));
  68.  
  69. $container->declare('evtinaKola','Car', array('normalGearNormalEngine'));
  70. $container->declare('evtinaKolaHubaviChasti','Car', array('skupGearNormalEngine'));
  71. $container->declare('skupaKola','Car', array('skupEngine'));
  72.  
  73. $e = $container->getObj('normalGearNormalEngine');
  74. echo $e->gears->price;
  75.  
  76. //$car = $container->getObj('skupaKola');
  77.  
  78.  
  79. //$car = new Car(new TurboEngine(new Gears()));
  80. //echo $car->engine->hp;
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
Runtime error #stdin #stdout #stderr 0s 82880KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Notice:  Use of undefined constant CheapGears - assumed 'CheapGears' in /home/s36Txx/prog.php on line 61
PHP Notice:  Use of undefined constant ExpensiveGears - assumed 'ExpensiveGears' in /home/s36Txx/prog.php on line 62
PHP Notice:  Use of undefined constant argsObjs - assumed 'argsObjs' in /home/s36Txx/prog.php on line 22
PHP Fatal error:  Uncaught TypeError: Argument 1 passed to ReflectionClass::newInstanceArgs() must be of the type array, string given in /home/s36Txx/prog.php:22
Stack trace:
#0 /home/s36Txx/prog.php(22): ReflectionClass->newInstanceArgs('argsObjs')
#1 /home/s36Txx/prog.php(18): DepInj->getObj('normalGear')
#2 /home/s36Txx/prog.php(73): DepInj->getObj('normalGearNorma...')
#3 {main}
  thrown in /home/s36Txx/prog.php on line 22