3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Immutable { private $_defaultValues = []; private $_userDefinedValues = []; private $_userDefinedProperties = []; private static $_doNotTakeOverProperties = [ '_defaultValues' => true, '_userDefinedValues' => true, '_userDefinedProperties' => true, ]; final public function __construct() { // take over all user-defined non-static properties foreach ((new \ReflectionObject($this))->getProperties() as $property) { $propertyName = $property->getName(); if (isset(self::$_doNotTakeOverProperties[$propertyName]) || $property->isStatic()) { continue; } $this->_userDefinedProperties[$propertyName] = true; $this->_defaultValues[$propertyName] = $property->getValue($this); unset($this->{$property->getName()}); } } final public function __set($name, $value) { if (!isset($this->_userDefinedProperties[$name])) { throw new \LogicException('Unknown property "' . $name . '"'); } if (array_key_exists($name, $this->_userDefinedValues)) { throw new \LogicException('You can not overwrite the value for property "' . $name . '"'); } $this->_userDefinedValues[$name] = $value; } final public function __get($name) { if (!isset($this->_userDefinedProperties[$name])) { throw new \LogicException('Unknown property "' . $name . '"'); } if (array_key_exists($name, $this->_userDefinedValues)) { return $this->_userDefinedValues[$name]; } return $this->_defaultValues[$name]; } } class Foo { use Immutable; public $poo; } $foo = new Foo; $foo->poo = 1; $foo->poo = 2;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bIJqi
function name:  (null)
number of ops:  9
compiled vars:  !0 = $foo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   DECLARE_CLASS                                            'foo'
   54     1        NEW                                              $1      'Foo'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
   55     4        ASSIGN_OBJ                                               !0, 'poo'
          5        OP_DATA                                                  1
   56     6        ASSIGN_OBJ                                               !0, 'poo'
          7        OP_DATA                                                  2
          8      > RETURN                                                   1

Class Immutable:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 33
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 33
Branch analysis from position: 8
2 jumps found. (Code = 47) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 19
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 17
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
filename:       /in/bIJqi
function name:  __construct
number of ops:  35
compiled vars:  !0 = $property, !1 = $propertyName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   NEW                                              $2      'ReflectionObject'
          1        FETCH_THIS                                       $3      
          2        SEND_VAR_EX                                              $3
          3        DO_FCALL                                      0          
          4        INIT_METHOD_CALL                                         $2, 'getProperties'
          5        DO_FCALL                                      0  $5      
          6      > FE_RESET_R                                       $6      $5, ->33
          7    > > FE_FETCH_R                                               $6, !0, ->33
   17     8    >   INIT_METHOD_CALL                                         !0, 'getName'
          9        DO_FCALL                                      0  $7      
         10        ASSIGN                                                   !1, $7
   18    11        FETCH_STATIC_PROP_IS                             ~9      '_doNotTakeOverProperties'
         12        ISSET_ISEMPTY_DIM_OBJ                         0  ~10     ~9, !1
         13      > JMPNZ_EX                                         ~10     ~10, ->17
         14    >   INIT_METHOD_CALL                                         !0, 'isStatic'
         15        DO_FCALL                                      0  $11     
         16        BOOL                                             ~10     $11
         17    > > JMPZ                                                     ~10, ->19
   19    18    > > JMP                                                      ->7
   21    19    >   FETCH_OBJ_W                                      $12     '_userDefinedProperties'
         20        ASSIGN_DIM                                               $12, !1
         21        OP_DATA                                                  <true>
   22    22        INIT_METHOD_CALL                                         !0, 'getValue'
         23        FETCH_THIS                                       $16     
         24        SEND_VAR_EX                                              $16
         25        DO_FCALL                                      0  $17     
         26        FETCH_OBJ_W                                      $14     '_defaultValues'
         27        ASSIGN_DIM                                               $14, !1
         28        OP_DATA                                                  $17
   23    29        INIT_METHOD_CALL                                         !0, 'getName'
         30        DO_FCALL                                      0  $18     
         31        UNSET_OBJ                                                $18
   16    32      > JMP                                                      ->7
         33    >   FE_FREE                                                  $6
   25    34      > RETURN                                                   null

End of function __construct

Function __set:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 12
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 21
Branch analysis from position: 15
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bIJqi
function name:  __set
number of ops:  25
compiled vars:  !0 = $name, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   28     2        FETCH_OBJ_IS                                     ~2      '_userDefinedProperties'
          3        ISSET_ISEMPTY_DIM_OBJ                         0  ~3      ~2, !0
          4        BOOL_NOT                                         ~4      ~3
          5      > JMPZ                                                     ~4, ->12
   29     6    >   NEW                                              $5      'LogicException'
          7        CONCAT                                           ~6      'Unknown+property+%22', !0
          8        CONCAT                                           ~7      ~6, '%22'
          9        SEND_VAL_EX                                              ~7
         10        DO_FCALL                                      0          
         11      > THROW                                         0          $5
   31    12    >   FETCH_OBJ_R                                      ~9      '_userDefinedValues'
         13        ARRAY_KEY_EXISTS                                         !0, ~9
         14      > JMPZ                                                     ~10, ->21
   32    15    >   NEW                                              $11     'LogicException'
         16        CONCAT                                           ~12     'You+can+not+overwrite+the+value+for+property+%22', !0
         17        CONCAT                                           ~13     ~12, '%22'
         18        SEND_VAL_EX                                              ~13
         19        DO_FCALL                                      0          
         20      > THROW                                         0          $11
   34    21    >   FETCH_OBJ_W                                      $15     '_userDefinedValues'
         22        ASSIGN_DIM                                               $15, !0
         23        OP_DATA                                                  !1
   35    24      > RETURN                                                   null

End of function __set

Function __get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bIJqi
function name:  __get
number of ops:  21
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   38     1        FETCH_OBJ_IS                                     ~1      '_userDefinedProperties'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~1, !0
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->11
   39     5    >   NEW                                              $4      'LogicException'
          6        CONCAT                                           ~5      'Unknown+property+%22', !0
          7        CONCAT                                           ~6      ~5, '%22'
          8        SEND_VAL_EX                                              ~6
          9        DO_FCALL                                      0          
         10      > THROW                                         0          $4
   41    11    >   FETCH_OBJ_R                                      ~8      '_userDefinedValues'
         12        ARRAY_KEY_EXISTS                                         !0, ~8
         13      > JMPZ                                                     ~9, ->17
   42    14    >   FETCH_OBJ_R                                      ~10     '_userDefinedValues'
         15        FETCH_DIM_R                                      ~11     ~10, !0
         16      > RETURN                                                   ~11
   44    17    >   FETCH_OBJ_R                                      ~12     '_defaultValues'
         18        FETCH_DIM_R                                      ~13     ~12, !0
         19      > RETURN                                                   ~13
   45    20*     > RETURN                                                   null

End of function __get

End of class Immutable.

Class Foo: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
169.16 ms | 1403 KiB | 13 Q