3v4l.org

run code in 300+ PHP versions simultaneously
<?php #[Attribute()] final class Sealed { public array $permitted = []; public function __construct(string ...$permitted) { $this->permitted = $permitted; } } function validate(string $classname): void { $class = new ReflectionClass($classname); $validate_against = static function($parent) use($classname) { if ($sealed = $parent->getAttributes(Sealed::class)) { if (!in_array($classname, $sealed[0]->newInstance()->permitted, true)) { throw new Error($classname.' cannot inherit from sealed class '.$parent->getName()); } } }; if ($parent = $class->getParentClass()) { $validate_against($parent); } foreach($class->getInterfaces() as $interface) { $validate_against($interface); } foreach($class->getTraits() as $trait) { $validate_against($trait); } echo 'ok'; } #[Sealed(Bar::class)] interface Foo { } final class Bar implements Foo {} final class Baz implements Foo {} validate(Bar::class); validate(Baz::class);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/uFnp8
function name:  (null)
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   DECLARE_CLASS                                            'bar'
   49     1        DECLARE_CLASS                                            'baz'
   51     2        INIT_FCALL                                               'validate'
          3        SEND_VAL                                                 'Bar'
          4        DO_FCALL                                      0          
   52     5        INIT_FCALL                                               'validate'
          6        SEND_VAL                                                 'Baz'
          7        DO_FCALL                                      0          
          8      > RETURN                                                   1

Function validate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 15
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 23
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 23
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 23
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 32
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 32
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
Branch analysis from position: 23
Branch analysis from position: 15
filename:       /in/uFnp8
function name:  validate
number of ops:  35
compiled vars:  !0 = $classname, !1 = $class, !2 = $validate_against, !3 = $parent, !4 = $interface, !5 = $trait
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   16     1        NEW                                              $6      'ReflectionClass'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $6
   18     5        DECLARE_LAMBDA_FUNCTION                          ~9      [0]
          6        BIND_LEXICAL                                             ~9, !0
          7        ASSIGN                                                   !2, ~9
   26     8        INIT_METHOD_CALL                                         !1, 'getParentClass'
          9        DO_FCALL                                      0  $11     
         10        ASSIGN                                           ~12     !3, $11
         11      > JMPZ                                                     ~12, ->15
   27    12    >   INIT_DYNAMIC_CALL                                        !2
         13        SEND_VAR_EX                                              !3
         14        DO_FCALL                                      0          
   30    15    >   INIT_METHOD_CALL                                         !1, 'getInterfaces'
         16        DO_FCALL                                      0  $14     
         17      > FE_RESET_R                                       $15     $14, ->23
         18    > > FE_FETCH_R                                               $15, !4, ->23
   31    19    >   INIT_DYNAMIC_CALL                                        !2
         20        SEND_VAR_EX                                              !4
         21        DO_FCALL                                      0          
   30    22      > JMP                                                      ->18
         23    >   FE_FREE                                                  $15
   34    24        INIT_METHOD_CALL                                         !1, 'getTraits'
         25        DO_FCALL                                      0  $17     
         26      > FE_RESET_R                                       $18     $17, ->32
         27    > > FE_FETCH_R                                               $18, !5, ->32
   35    28    >   INIT_DYNAMIC_CALL                                        !2
         29        SEND_VAR_EX                                              !5
         30        DO_FCALL                                      0          
   34    31      > JMP                                                      ->27
         32    >   FE_FREE                                                  $18
   38    33        ECHO                                                     'ok'
   39    34      > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 26
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 26
Branch analysis from position: 18
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
filename:       /in/uFnp8
function name:  {closure}
number of ops:  27
compiled vars:  !0 = $parent, !1 = $classname, !2 = $sealed
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   19     2        INIT_METHOD_CALL                                         !0, 'getAttributes'
          3        SEND_VAL_EX                                              'Sealed'
          4        DO_FCALL                                      0  $3      
          5        ASSIGN                                           ~4      !2, $3
          6      > JMPZ                                                     ~4, ->26
   20     7    >   INIT_FCALL                                               'in_array'
          8        SEND_VAR                                                 !1
          9        FETCH_DIM_R                                      ~5      !2, 0
         10        INIT_METHOD_CALL                                         ~5, 'newInstance'
         11        DO_FCALL                                      0  $6      
         12        FETCH_OBJ_R                                      ~7      $6, 'permitted'
         13        SEND_VAL                                                 ~7
         14        SEND_VAL                                                 <true>
         15        DO_ICALL                                         $8      
         16        BOOL_NOT                                         ~9      $8
         17      > JMPZ                                                     ~9, ->26
   21    18    >   NEW                                              $10     'Error'
         19        CONCAT                                           ~11     !1, '+cannot+inherit+from+sealed+class+'
         20        INIT_METHOD_CALL                                         !0, 'getName'
         21        DO_FCALL                                      0  $12     
         22        CONCAT                                           ~13     ~11, $12
         23        SEND_VAL_EX                                              ~13
         24        DO_FCALL                                      0          
         25      > THROW                                         0          $10
   24    26    > > RETURN                                                   null

End of Dynamic Function 0

End of function validate

Class Sealed:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/uFnp8
function name:  __construct
number of ops:  4
compiled vars:  !0 = $permitted
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV_VARIADIC                                    !0      
    9     1        ASSIGN_OBJ                                               'permitted'
          2        OP_DATA                                                  !0
   10     3      > RETURN                                                   null

End of function __construct

End of class Sealed.

Class Foo: [no user functions]
Class Bar: [no user functions]
Class Baz: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
171.95 ms | 1448 KiB | 16 Q