3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Request { public $answer = 42; public function __construct() { echo 'Request created', PHP_EOL; } } class DI { protected $shared = []; protected $definitions = []; public function __construct() {} public function set($name, $definition, $shared = false) { if($shared) { if(isset($shared[$name])) { throw new Exception("Definition '$name' is already set. Use DI::remove('$name') to remove it."); } $this->shared[$name] = $definition; } else { if(isset($definitions[$name])) { throw new Exception("Definition '$name' is already set. Use DI::remove('$name') to remove it."); } $this->definitions[$name] = $definition; } } public function getRaw($name) { if(!isset($this->definitions[$name])) { throw new Exception("Definition '$name' is not set. Use DI::set('$name', <definition>) to set it."); } return $this->definitions[$name]; } public function get($name) { $definition = $this->getRaw($name); if(is_callable($definition)) return $definition(); if(class_exists($definition)) return new $definition(); } public function getShared($name) { if(!isset($this->shared[$name])) { throw new Exception("Shared definition '$name' is not set. Use DI::set('$name', <definition>, true) to set it."); } $shared = $this->shared[$name]; if(is_callable($shared)) $this->shared[$name] = $shared(); else if(is_string($shared) && class_exists($shared)) $this->shared[$name] = new $shared(); return $this->shared[$name]; } } $di = new DI(); //Using an anonymous function $di->set('request1', function(){ return new Request(); }); //Using a class name non-shared $di->set('request2', 'Request', true); $request = $di->getShared('request2'); $request->answer = 5; var_dump($di->getShared('request2')); $request = $di->get('request1'); $request->answer = 5; var_dump($di->get('request1'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fnRTh
function name:  (null)
number of ops:  38
compiled vars:  !0 = $di, !1 = $request
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   NEW                                              $2      'DI'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   61     3        INIT_METHOD_CALL                                         !0, 'set'
          4        SEND_VAL_EX                                              'request1'
          5        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FfnRTh%3A61%240'
   63     6        SEND_VAL_EX                                              ~5
          7        DO_FCALL                                      0          
   66     8        INIT_METHOD_CALL                                         !0, 'set'
          9        SEND_VAL_EX                                              'request2'
         10        SEND_VAL_EX                                              'Request'
         11        SEND_VAL_EX                                              <true>
         12        DO_FCALL                                      0          
   68    13        INIT_METHOD_CALL                                         !0, 'getShared'
         14        SEND_VAL_EX                                              'request2'
         15        DO_FCALL                                      0  $8      
         16        ASSIGN                                                   !1, $8
   69    17        ASSIGN_OBJ                                               !1, 'answer'
         18        OP_DATA                                                  5
   70    19        INIT_FCALL                                               'var_dump'
         20        INIT_METHOD_CALL                                         !0, 'getShared'
         21        SEND_VAL_EX                                              'request2'
         22        DO_FCALL                                      0  $11     
         23        SEND_VAR                                                 $11
         24        DO_ICALL                                                 
   72    25        INIT_METHOD_CALL                                         !0, 'get'
         26        SEND_VAL_EX                                              'request1'
         27        DO_FCALL                                      0  $13     
         28        ASSIGN                                                   !1, $13
   73    29        ASSIGN_OBJ                                               !1, 'answer'
         30        OP_DATA                                                  5
   74    31        INIT_FCALL                                               'var_dump'
         32        INIT_METHOD_CALL                                         !0, 'get'
         33        SEND_VAL_EX                                              'request1'
         34        DO_FCALL                                      0  $16     
         35        SEND_VAR                                                 $16
         36        DO_ICALL                                                 
         37      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FfnRTh%3A61%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fnRTh
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   NEW                                              $0      'Request'
          1        DO_FCALL                                      0          
          2      > RETURN                                                   $0
   63     3*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FfnRTh%3A61%240

Class Request:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fnRTh
function name:  __construct
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   ECHO                                                     'Request+created'
          1        ECHO                                                     '%0A'
    7     2      > RETURN                                                   null

End of function __construct

End of class Request.

Class DI:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fnRTh
function name:  __construct
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E > > RETURN                                                   null

End of function __construct

Function set:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 19
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 15
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 30
Branch analysis from position: 21
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fnRTh
function name:  set
number of ops:  34
compiled vars:  !0 = $name, !1 = $definition, !2 = $shared, !3 = $definitions
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <false>
   17     3      > JMPZ                                                     !2, ->19
   18     4    >   ISSET_ISEMPTY_DIM_OBJ                         0          !2, !0
          5      > JMPZ                                                     ~4, ->15
   19     6    >   NEW                                              $5      'Exception'
          7        ROPE_INIT                                     5  ~7      'Definition+%27'
          8        ROPE_ADD                                      1  ~7      ~7, !0
          9        ROPE_ADD                                      2  ~7      ~7, '%27+is+already+set.+Use+DI%3A%3Aremove%28%27'
         10        ROPE_ADD                                      3  ~7      ~7, !0
         11        ROPE_END                                      4  ~6      ~7, '%27%29+to+remove+it.'
         12        SEND_VAL_EX                                              ~6
         13        DO_FCALL                                      0          
         14      > THROW                                         0          $5
   21    15    >   FETCH_OBJ_W                                      $11     'shared'
         16        ASSIGN_DIM                                               $11, !0
         17        OP_DATA                                                  !1
         18      > JMP                                                      ->33
   23    19    >   ISSET_ISEMPTY_DIM_OBJ                         0          !3, !0
         20      > JMPZ                                                     ~13, ->30
   24    21    >   NEW                                              $14     'Exception'
         22        ROPE_INIT                                     5  ~16     'Definition+%27'
         23        ROPE_ADD                                      1  ~16     ~16, !0
         24        ROPE_ADD                                      2  ~16     ~16, '%27+is+already+set.+Use+DI%3A%3Aremove%28%27'
         25        ROPE_ADD                                      3  ~16     ~16, !0
         26        ROPE_END                                      4  ~15     ~16, '%27%29+to+remove+it.'
         27        SEND_VAL_EX                                              ~15
         28        DO_FCALL                                      0          
         29      > THROW                                         0          $14
   26    30    >   FETCH_OBJ_W                                      $20     'definitions'
         31        ASSIGN_DIM                                               $20, !0
         32        OP_DATA                                                  !1
   28    33    > > RETURN                                                   null

End of function set

Function getraw:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 14
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fnRTh
function name:  getRaw
number of ops:  18
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   31     1        FETCH_OBJ_IS                                     ~1      'definitions'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~1, !0
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->14
   32     5    >   NEW                                              $4      'Exception'
          6        ROPE_INIT                                     5  ~6      'Definition+%27'
          7        ROPE_ADD                                      1  ~6      ~6, !0
          8        ROPE_ADD                                      2  ~6      ~6, '%27+is+not+set.+Use+DI%3A%3Aset%28%27'
          9        ROPE_ADD                                      3  ~6      ~6, !0
         10        ROPE_END                                      4  ~5      ~6, '%27%2C+%3Cdefinition%3E%29+to+set+it.'
         11        SEND_VAL_EX                                              ~5
         12        DO_FCALL                                      0          
         13      > THROW                                         0          $4
   35    14    >   FETCH_OBJ_R                                      ~10     'definitions'
         15        FETCH_DIM_R                                      ~11     ~10, !0
         16      > RETURN                                                   ~11
   36    17*     > RETURN                                                   null

End of function getraw

Function get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 20
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fnRTh
function name:  get
number of ops:  21
compiled vars:  !0 = $name, !1 = $definition
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
   39     1        INIT_METHOD_CALL                                         'getRaw'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !1, $2
   40     5        INIT_FCALL                                               'is_callable'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $4      
          8      > JMPZ                                                     $4, ->12
          9    >   INIT_DYNAMIC_CALL                                        !1
         10        DO_FCALL                                      0  $5      
         11      > RETURN                                                   $5
   41    12    >   INIT_FCALL                                               'class_exists'
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                         $6      
         15      > JMPZ                                                     $6, ->20
         16    >   FETCH_CLASS                                   0  $7      !1
         17        NEW                                              $8      $7
         18        DO_FCALL                                      0          
         19      > RETURN                                                   $8
   42    20    > > RETURN                                                   null

End of function get

Function getshared:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 14
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 27
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
2 jumps found. (Code = 46) Position 1 = 29, Position 2 = 33
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 40
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
Branch analysis from position: 33
filename:       /in/fnRTh
function name:  getShared
number of ops:  44
compiled vars:  !0 = $name, !1 = $shared
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   45     1        FETCH_OBJ_IS                                     ~2      'shared'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~3      ~2, !0
          3        BOOL_NOT                                         ~4      ~3
          4      > JMPZ                                                     ~4, ->14
   46     5    >   NEW                                              $5      'Exception'
          6        ROPE_INIT                                     5  ~7      'Shared+definition+%27'
          7        ROPE_ADD                                      1  ~7      ~7, !0
          8        ROPE_ADD                                      2  ~7      ~7, '%27+is+not+set.+Use+DI%3A%3Aset%28%27'
          9        ROPE_ADD                                      3  ~7      ~7, !0
         10        ROPE_END                                      4  ~6      ~7, '%27%2C+%3Cdefinition%3E%2C+true%29+to+set+it.'
         11        SEND_VAL_EX                                              ~6
         12        DO_FCALL                                      0          
         13      > THROW                                         0          $5
   48    14    >   FETCH_OBJ_R                                      ~11     'shared'
         15        FETCH_DIM_R                                      ~12     ~11, !0
         16        ASSIGN                                                   !1, ~12
   49    17        INIT_FCALL                                               'is_callable'
         18        SEND_VAR                                                 !1
         19        DO_ICALL                                         $14     
         20      > JMPZ                                                     $14, ->27
         21    >   INIT_DYNAMIC_CALL                                        !1
         22        DO_FCALL                                      0  $17     
         23        FETCH_OBJ_W                                      $15     'shared'
         24        ASSIGN_DIM                                               $15, !0
         25        OP_DATA                                                  $17
         26      > JMP                                                      ->40
   50    27    >   TYPE_CHECK                                   64  ~18     !1
         28      > JMPZ_EX                                          ~18     ~18, ->33
         29    >   INIT_FCALL                                               'class_exists'
         30        SEND_VAR                                                 !1
         31        DO_ICALL                                         $19     
         32        BOOL                                             ~18     $19
         33    > > JMPZ                                                     ~18, ->40
         34    >   FETCH_CLASS                                   0  $22     !1
         35        NEW                                              $23     $22
         36        DO_FCALL                                      0          
         37        FETCH_OBJ_W                                      $20     'shared'
         38        ASSIGN_DIM                                               $20, !0
         39        OP_DATA                                                  $23
   52    40    >   FETCH_OBJ_R                                      ~25     'shared'
         41        FETCH_DIM_R                                      ~26     ~25, !0
         42      > RETURN                                                   ~26
   53    43*     > RETURN                                                   null

End of function getshared

End of class DI.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
191.57 ms | 1404 KiB | 19 Q