3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface ConfigValueInterface { public function __toString(); } interface ConfigWriterInterface { public function write(ConfigValueInterface $value); } class WriteManager { protected $writers = []; public function addWriter(ConfigWriterInterface $writer, $value_class) { $this->writers[$value_class] = $writer; } public function write(ConfigValueInterface $value) { $class = get_class($value); if (isset($this->writers[$class])) { $this->writers[$class]->write($value); } } } class BaseConfigValue implements ConfigValueInterface { public $storage_location; public function __toString() { return "derp"; } } class NginxConfigValue extends BaseConfigValue {} class BaseConfigWriter implements ConfigWriterInterface { public function write(ConfigValueInterface $value) { if (!$value instanceof BaseConfigValue) { throw new \RuntimeException('Invalid value type'); } echo "writing to {$value->storage_location}\n"; } } class NginxConfigWriter implements ConfigWriterInterface { public function write(ConfigValueInterface $value) { if (!$value instanceof NginxConfigValue) { throw new \RuntimeException('Invalid value type'); } echo "writing NGINX config to {$value->storage_location}\n"; } } // Mock Value objects $nginx_value = new NginxConfigValue(); $base_value = new BaseConfigValue(); $nginx_value->storage_location = $base_value->storage_location = "/some/path"; // Set up manager $manager = new WriteManager(); $manager->addWriter(BaseConfigWriter::class, BaseConfigValue::class); $manager->addWriter(NginxConfigWriter::class, NginxConfigValue::class); $generator = new ExampleGenerator(); $value = $generator->generate(); $manager->write($value); $generator = new NginxGenerator(); $value = $generator->generate(); $manager->write($value);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MvEsG
function name:  (null)
number of ops:  45
compiled vars:  !0 = $nginx_value, !1 = $base_value, !2 = $manager, !3 = $generator, !4 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                            'configvalueinterface'
   35     1        DECLARE_CLASS                                            'baseconfigvalue'
   47     2        DECLARE_CLASS                                            'nginxconfigvalue', 'baseconfigvalue'
   49     3        DECLARE_CLASS                                            'baseconfigwriter'
   62     4        DECLARE_CLASS                                            'nginxconfigwriter'
   77     5        NEW                                              $5      'NginxConfigValue'
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !0, $5
   78     8        NEW                                              $8      'BaseConfigValue'
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !1, $8
   80    11        ASSIGN_OBJ                                       ~12     !1, 'storage_location'
         12        OP_DATA                                                  '%2Fsome%2Fpath'
         13        ASSIGN_OBJ                                               !0, 'storage_location'
         14        OP_DATA                                                  ~12
   83    15        NEW                                              $13     'WriteManager'
         16        DO_FCALL                                      0          
         17        ASSIGN                                                   !2, $13
   84    18        INIT_METHOD_CALL                                         !2, 'addWriter'
         19        SEND_VAL_EX                                              'BaseConfigWriter'
         20        SEND_VAL_EX                                              'BaseConfigValue'
         21        DO_FCALL                                      0          
   85    22        INIT_METHOD_CALL                                         !2, 'addWriter'
         23        SEND_VAL_EX                                              'NginxConfigWriter'
         24        SEND_VAL_EX                                              'NginxConfigValue'
         25        DO_FCALL                                      0          
   88    26        NEW                                              $18     'ExampleGenerator'
         27        DO_FCALL                                      0          
         28        ASSIGN                                                   !3, $18
   89    29        INIT_METHOD_CALL                                         !3, 'generate'
         30        DO_FCALL                                      0  $21     
         31        ASSIGN                                                   !4, $21
   90    32        INIT_METHOD_CALL                                         !2, 'write'
         33        SEND_VAR_EX                                              !4
         34        DO_FCALL                                      0          
   93    35        NEW                                              $24     'NginxGenerator'
         36        DO_FCALL                                      0          
         37        ASSIGN                                                   !3, $24
   94    38        INIT_METHOD_CALL                                         !3, 'generate'
         39        DO_FCALL                                      0  $27     
         40        ASSIGN                                                   !4, $27
   95    41        INIT_METHOD_CALL                                         !2, 'write'
         42        SEND_VAR_EX                                              !4
         43        DO_FCALL                                      0          
         44      > RETURN                                                   1

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

End of function __tostring

End of class ConfigValueInterface.

Class ConfigWriterInterface:
Function write:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MvEsG
function name:  write
number of ops:  2
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function write

End of class ConfigWriterInterface.

Class WriteManager:
Function addwriter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MvEsG
function name:  addWriter
number of ops:  6
compiled vars:  !0 = $writer, !1 = $value_class
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   22     2        FETCH_OBJ_W                                      $2      'writers'
          3        ASSIGN_DIM                                               $2, !1
          4        OP_DATA                                                  !0
   23     5      > RETURN                                                   null

End of function addwriter

Function write:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/MvEsG
function name:  write
number of ops:  12
compiled vars:  !0 = $value, !1 = $class
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   27     1        GET_CLASS                                        ~2      !0
          2        ASSIGN                                                   !1, ~2
   28     3        FETCH_OBJ_IS                                     ~4      'writers'
          4        ISSET_ISEMPTY_DIM_OBJ                         0          ~4, !1
          5      > JMPZ                                                     ~5, ->11
   29     6    >   FETCH_OBJ_R                                      ~6      'writers'
          7        FETCH_DIM_R                                      ~7      ~6, !1
          8        INIT_METHOD_CALL                                         ~7, 'write'
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0          
   31    11    > > RETURN                                                   null

End of function write

End of class WriteManager.

Class BaseConfigValue:
Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MvEsG
function name:  __toString
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E > > RETURN                                                   'derp'
   43     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function __tostring

End of class BaseConfigValue.

Class NginxConfigValue: [no user functions]
Class BaseConfigWriter:
Function write:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MvEsG
function name:  write
number of ops:  14
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   54     1        INSTANCEOF                                       ~1      !0, 'BaseConfigValue'
          2        BOOL_NOT                                         ~2      ~1
          3      > JMPZ                                                     ~2, ->8
   55     4    >   NEW                                              $3      'RuntimeException'
          5        SEND_VAL_EX                                              'Invalid+value+type'
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $3
   57     8    >   ROPE_INIT                                     3  ~7      'writing+to+'
          9        FETCH_OBJ_R                                      ~5      !0, 'storage_location'
         10        ROPE_ADD                                      1  ~7      ~7, ~5
         11        ROPE_END                                      2  ~6      ~7, '%0A'
         12        ECHO                                                     ~6
   58    13      > RETURN                                                   null

End of function write

End of class BaseConfigWriter.

Class NginxConfigWriter:
Function write:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MvEsG
function name:  write
number of ops:  14
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
   67     1        INSTANCEOF                                       ~1      !0, 'NginxConfigValue'
          2        BOOL_NOT                                         ~2      ~1
          3      > JMPZ                                                     ~2, ->8
   68     4    >   NEW                                              $3      'RuntimeException'
          5        SEND_VAL_EX                                              'Invalid+value+type'
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $3
   70     8    >   ROPE_INIT                                     3  ~7      'writing+NGINX+config+to+'
          9        FETCH_OBJ_R                                      ~5      !0, 'storage_location'
         10        ROPE_ADD                                      1  ~7      ~7, ~5
         11        ROPE_END                                      2  ~6      ~7, '%0A'
         12        ECHO                                                     ~6
   71    13      > RETURN                                                   null

End of function write

End of class NginxConfigWriter.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.13 ms | 1398 KiB | 13 Q