3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class Options { // This is OK but why redundant initialization? // public array $stack = []; public array $stack; public function __construct(array $stack) { $this->stack = $stack; } public function __set(string $name, $value) { // if (empty($this->stack)) { // // This line below yields: object(acme\Options)#1 (0) { ["stack"]=> uninitialized(array) }. // // return; // // This line below yields: object(acme\Options)#1 (1) { ["stack"]=> array(1) { ["stack"]=> array(1) { ["one"]=> int(1) } } }. // // $this->stack = []; // } // This is solving problem but the purpose is not that also corrupting $stack structure inserting a new sub-array. // object(acme\Options)#1 (1) { ["stack"]=> array(2) { ["one"]=> int(1) ["stack"]=> array(1) { ["one"]=> int(1) } } } // $this->stack = $value; // This is indicating that __set called before (before __construct). // throw new \Exception(); // This is problematic part. $this->stack[$name] = $value; } public function __get(string $name) { // This is indicating that __get called before (before __construct). // throw new \Exception(); return $this->stack[$name] ?? null; } } var_dump(new Options(['one' => 1]));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VJFBK
function name:  (null)
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   INIT_FCALL                                               'var_dump'
          1        NEW                                              $0      'Options'
          2        SEND_VAL_EX                                              <array>
          3        DO_FCALL                                      0          
          4        SEND_VAR                                                 $0
          5        DO_ICALL                                                 
          6      > RETURN                                                   1

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

End of function __construct

Function __set:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VJFBK
function name:  __set
number of ops:  6
compiled vars:  !0 = $name, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   28     2        FETCH_OBJ_W                                      $2      'stack'
          3        ASSIGN_DIM                                               $2, !0
          4        OP_DATA                                                  !1
   29     5      > 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/VJFBK
function name:  __get
number of ops:  7
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   35     1        FETCH_OBJ_IS                                     ~1      'stack'
          2        FETCH_DIM_IS                                     ~2      ~1, !0
          3        COALESCE                                         ~3      ~2
          4        QM_ASSIGN                                        ~3      null
          5      > RETURN                                                   ~3
   36     6*     > RETURN                                                   null

End of function __get

End of class Options.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
175.34 ms | 1396 KiB | 15 Q