3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Request { 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>, <shared>) to set it."); } return $this->definitions[$name]; } public function get($name) { $definition = $this->getRaw($name); echo is_callable($definition) ? 'Callable def' : 'Non-calleable def'; echo is_string($definition) ? 'Class' : 'non-class'; die; return $definition(); } } $di = new DI(); //Using an anonymous function $di->set('request', function(){ return new Request(); }); //Using an anonymous function $di->set('request', 'Request'); var_dump($di->get('request'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sgoem
function name:  (null)
number of ops:  19
compiled vars:  !0 = $di
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   NEW                                              $1      'DI'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   51     3        INIT_METHOD_CALL                                         !0, 'set'
          4        SEND_VAL_EX                                              'request'
          5        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fsgoem%3A51%240'
   53     6        SEND_VAL_EX                                              ~4
          7        DO_FCALL                                      0          
   56     8        INIT_METHOD_CALL                                         !0, 'set'
          9        SEND_VAL_EX                                              'request'
         10        SEND_VAL_EX                                              'Request'
         11        DO_FCALL                                      0          
   58    12        INIT_FCALL                                               'var_dump'
         13        INIT_METHOD_CALL                                         !0, 'get'
         14        SEND_VAL_EX                                              'request'
         15        DO_FCALL                                      0  $7      
         16        SEND_VAR                                                 $7
         17        DO_ICALL                                                 
         18      > RETURN                                                   1

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

End of function %00%7Bclosure%7D%2Fin%2Fsgoem%3A51%240

Class Request:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sgoem
function name:  __construct
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   ECHO                                                     'Request+created'
          1        ECHO                                                     '%0A'
    6     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/sgoem
function name:  __construct
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     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/sgoem
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
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <false>
   16     3      > JMPZ                                                     !2, ->19
   17     4    >   ISSET_ISEMPTY_DIM_OBJ                         0          !2, !0
          5      > JMPZ                                                     ~4, ->15
   18     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
   20    15    >   FETCH_OBJ_W                                      $11     'shared'
         16        ASSIGN_DIM                                               $11, !0
         17        OP_DATA                                                  !1
         18      > JMP                                                      ->33
   22    19    >   ISSET_ISEMPTY_DIM_OBJ                         0          !3, !0
         20      > JMPZ                                                     ~13, ->30
   23    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
   25    30    >   FETCH_OBJ_W                                      $20     'definitions'
         31        ASSIGN_DIM                                               $20, !0
         32        OP_DATA                                                  !1
   27    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/sgoem
function name:  getRaw
number of ops:  18
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
   30     1        FETCH_OBJ_IS                                     ~1      'definitions'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~1, !0
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->14
   31     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%2C+%3Cshared%3E%29+to+set+it.'
         11        SEND_VAL_EX                                              ~5
         12        DO_FCALL                                      0          
         13      > THROW                                         0          $4
   34    14    >   FETCH_OBJ_R                                      ~10     'definitions'
         15        FETCH_DIM_R                                      ~11     ~10, !0
         16      > RETURN                                                   ~11
   35    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 = 11
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 17
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 17
Branch analysis from position: 15
Branch analysis from position: 17
filename:       /in/sgoem
function name:  get
number of ops:  24
compiled vars:  !0 = $name, !1 = $definition
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
   38     1        INIT_METHOD_CALL                                         'getRaw'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !1, $2
   39     5        INIT_FCALL                                               'is_callable'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $4      
          8      > JMPZ                                                     $4, ->11
          9    >   QM_ASSIGN                                        ~5      'Callable+def'
         10      > JMP                                                      ->12
         11    >   QM_ASSIGN                                        ~5      'Non-calleable+def'
         12    >   ECHO                                                     ~5
   40    13        TYPE_CHECK                                   64          !1
         14      > JMPZ                                                     ~6, ->17
         15    >   QM_ASSIGN                                        ~7      'Class'
         16      > JMP                                                      ->18
         17    >   QM_ASSIGN                                        ~7      'non-class'
         18    >   ECHO                                                     ~7
   41    19      > EXIT                                                     
   42    20*       INIT_DYNAMIC_CALL                                        !1
         21*       DO_FCALL                                      0  $8      
         22*       RETURN                                                   $8
   43    23*     > RETURN                                                   null

End of function get

End of class DI.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.07 ms | 1400 KiB | 17 Q