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}"; } } 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}"; } } class ExampleGenerator { public function generate() { $value = new BaseConfigValue(); $value->storage_location = "/some/path/sample.config"; return $value; } } class NginxGenerator { public function generate() { $value = new NginxConfigValue(); $value->storage_location = "/some/path/sample.config"; return $value; } } // Set up manager $manager = new WriteManager(); $manager->addWriter(new BaseConfigWriter, BaseConfigValue::class); $manager->addWriter(new NginxConfigWriter, BaseConfigValue::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/RQCMY
function name:  (null)
number of ops:  39
compiled vars:  !0 = $manager, !1 = $generator, !2 = $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'
  104     5        NEW                                              $3      'WriteManager'
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !0, $3
  105     8        INIT_METHOD_CALL                                         !0, 'addWriter'
          9        NEW                                              $6      'BaseConfigWriter'
         10        DO_FCALL                                      0          
         11        SEND_VAR_NO_REF_EX                                       $6
         12        SEND_VAL_EX                                              'BaseConfigValue'
         13        DO_FCALL                                      0          
  106    14        INIT_METHOD_CALL                                         !0, 'addWriter'
         15        NEW                                              $9      'NginxConfigWriter'
         16        DO_FCALL                                      0          
         17        SEND_VAR_NO_REF_EX                                       $9
         18        SEND_VAL_EX                                              'BaseConfigValue'
         19        DO_FCALL                                      0          
  109    20        NEW                                              $12     'ExampleGenerator'
         21        DO_FCALL                                      0          
         22        ASSIGN                                                   !1, $12
  110    23        INIT_METHOD_CALL                                         !1, 'generate'
         24        DO_FCALL                                      0  $15     
         25        ASSIGN                                                   !2, $15
  111    26        INIT_METHOD_CALL                                         !0, 'write'
         27        SEND_VAR_EX                                              !2
         28        DO_FCALL                                      0          
  114    29        NEW                                              $18     'NginxGenerator'
         30        DO_FCALL                                      0          
         31        ASSIGN                                                   !1, $18
  115    32        INIT_METHOD_CALL                                         !1, 'generate'
         33        DO_FCALL                                      0  $21     
         34        ASSIGN                                                   !2, $21
  116    35        INIT_METHOD_CALL                                         !0, 'write'
         36        SEND_VAR_EX                                              !2
         37        DO_FCALL                                      0          
         38      > RETURN                                                   1

Class ConfigValueInterface:
Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RQCMY
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/RQCMY
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/RQCMY
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/RQCMY
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/RQCMY
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/RQCMY
function name:  write
number of ops:  13
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    >   NOP                                                      
          9        FETCH_OBJ_R                                      ~5      !0, 'storage_location'
         10        FAST_CONCAT                                      ~6      'writing+to+', ~5
         11        ECHO                                                     ~6
   58    12      > 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/RQCMY
function name:  write
number of ops:  13
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    >   NOP                                                      
          9        FETCH_OBJ_R                                      ~5      !0, 'storage_location'
         10        FAST_CONCAT                                      ~6      'writing+NGINX+config+to+', ~5
         11        ECHO                                                     ~6
   71    12      > RETURN                                                   null

End of function write

End of class NginxConfigWriter.

Class ExampleGenerator:
Function generate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RQCMY
function name:  generate
number of ops:  7
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   NEW                                              $1      'BaseConfigValue'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   82     3        ASSIGN_OBJ                                               !0, 'storage_location'
          4        OP_DATA                                                  '%2Fsome%2Fpath%2Fsample.config'
   84     5      > RETURN                                                   !0
   85     6*     > RETURN                                                   null

End of function generate

End of class ExampleGenerator.

Class NginxGenerator:
Function generate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RQCMY
function name:  generate
number of ops:  7
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   95     0  E >   NEW                                              $1      'NginxConfigValue'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   96     3        ASSIGN_OBJ                                               !0, 'storage_location'
          4        OP_DATA                                                  '%2Fsome%2Fpath%2Fsample.config'
   98     5      > RETURN                                                   !0
   99     6*     > RETURN                                                   null

End of function generate

End of class NginxGenerator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.77 ms | 1407 KiB | 13 Q