3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Immutable { private static $sealed = array(); public function __set($name, $value) { if (isset(self::$sealed[spl_object_hash($this)])) { throw new Exception("can't touch this, nah nah nuh nahhh"); } $this->$name = $value; } public function __get($name) { return $this->$name; } protected function seal() { self::$sealed[spl_object_hash($this)] = true; } } class ImmutableThingy { use Immutable; protected $foo; public function __construct($foo) { $this->foo = $foo; $this->seal(); // What the fuck ever broh } } $thing = new ImmutableThingy("bar"); var_dump($thing, $thing->foo); $thing->foo = 'baz'; var_dump($thing, $thing->foo);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DAYom
function name:  (null)
number of ops:  18
compiled vars:  !0 = $thing
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   DECLARE_CLASS                                            'immutablethingy'
   36     1        NEW                                              $1      'ImmutableThingy'
          2        SEND_VAL_EX                                              'bar'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $1
   38     5        INIT_FCALL                                               'var_dump'
          6        SEND_VAR                                                 !0
          7        FETCH_OBJ_R                                      ~4      !0, 'foo'
          8        SEND_VAL                                                 ~4
          9        DO_ICALL                                                 
   40    10        ASSIGN_OBJ                                               !0, 'foo'
         11        OP_DATA                                                  'baz'
   42    12        INIT_FCALL                                               'var_dump'
         13        SEND_VAR                                                 !0
         14        FETCH_OBJ_R                                      ~7      !0, 'foo'
         15        SEND_VAL                                                 ~7
         16        DO_ICALL                                                 
         17      > RETURN                                                   1

Class Immutable:
Function __set:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DAYom
function name:  __set
number of ops:  16
compiled vars:  !0 = $name, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    7     2        INIT_FCALL                                               'spl_object_hash'
          3        FETCH_THIS                                       ~3      
          4        SEND_VAL                                                 ~3
          5        DO_ICALL                                         $4      
          6        FETCH_STATIC_PROP_IS                             ~2      'sealed'
          7        ISSET_ISEMPTY_DIM_OBJ                         0          ~2, $4
          8      > JMPZ                                                     ~5, ->13
    8     9    >   NEW                                              $6      'Exception'
         10        SEND_VAL_EX                                              'can%27t+touch+this%2C+nah+nah+nuh+nahhh'
         11        DO_FCALL                                      0          
         12      > THROW                                         0          $6
   10    13    >   ASSIGN_OBJ                                               !0
         14        OP_DATA                                                  !1
   11    15      > RETURN                                                   null

End of function __set

Function __get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DAYom
function name:  __get
number of ops:  4
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   14     1        FETCH_OBJ_R                                      ~1      !0
          2      > RETURN                                                   ~1
   15     3*     > RETURN                                                   null

End of function __get

Function seal:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DAYom
function name:  seal
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   INIT_FCALL                                               'spl_object_hash'
          1        FETCH_THIS                                       ~1      
          2        SEND_VAL                                                 ~1
          3        DO_ICALL                                         $2      
          4        FETCH_STATIC_PROP_W          global              $0      'sealed'
          5        ASSIGN_DIM                                               $0, $2
          6        OP_DATA                                                  <true>
   19     7      > RETURN                                                   null

End of function seal

End of class Immutable.

Class ImmutableThingy:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DAYom
function name:  __construct
number of ops:  6
compiled vars:  !0 = $foo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   29     1        ASSIGN_OBJ                                               'foo'
          2        OP_DATA                                                  !0
   30     3        INIT_METHOD_CALL                                         'seal'
          4        DO_FCALL                                      0          
   33     5      > RETURN                                                   null

End of function __construct

End of class ImmutableThingy.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
191.96 ms | 1400 KiB | 17 Q