3v4l.org

run code in 300+ PHP versions simultaneously
<?php // modified from: http://us1.php.net/manual/en/class.arrayaccess.php#99236 // sanity and error checking omitted for brevity // note: it's a good idea to implement arrayaccess + countable + // an iterator interface (like iteratoraggregate) as a triplet /* // WORKS. class Template implements ArrayAccess { // makes it so we can do $template_inst['foo']; public $config = array(); // necessary for deep copies public function __clone() { foreach ($this->config as $key => $value) if ($value instanceof self) $this[$key] = clone $value; } public function __construct(array $config = array()) { foreach ($config as $key => $value) $this[$key] = $value; } public function offsetSet($offset, $config) { if (is_array($config)) { $config = new self($config); } if ($offset === null) { // don't forget this! $this->config[] = $config; } else { $this->config[$offset] = $config; } } public function toArray() { $config = $this->config; foreach ($config as $key => $value) if ($value instanceof self) $config[$key] = $value->toArray(); return $config; } // as normal public function offsetGet($offset) { return $this->config[$offset]; } public function offsetExists($offset) { return isset($this->config[$offset]); } public function offsetUnset($offset) { unset($this->config); } } */ // DOESN'T WORK class Template implements ArrayAccess { // makes it so we can do $template_inst['foo']; // container for items passed with null $offset: $this[] = $value; public $implied_key_list = array(); // necessary for deep copies public function __clone() { foreach ($this as $key => $value) if ($value instanceof self) $this[$key] = clone $value; } public function __construct(array $items = array()) { foreach ($items as $key => $value) { $this[$key] = $value; } } public function offsetSet($offset, $value) { if (is_array($value)) { $value = new self(array($offset => $value)); } # segfault from this line if ($offset === null) { // don't forget this! allows `$this[] = $value;` $this->implied_key_list[] = $value; } else { $this->$offset = $value; } } // as normal public function offsetGet($offset) { return $this->$offset; } public function offsetExists($offset) { return isset($this->$offset); } public function offsetUnset($offset) { unset($this->$offset); } } $test = new Template(array('noises' => array('dog' => 'woof'))); echo "<pre><code>"; print_r($test); echo "</code></pre>";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bTcrJ
function name:  (null)
number of ops:  11
compiled vars:  !0 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   DECLARE_CLASS                                            'template'
   82     1        NEW                                              $1      'Template'
          2        SEND_VAL_EX                                              <array>
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $1
   84     5        ECHO                                                     '%3Cpre%3E%3Ccode%3E'
   85     6        INIT_FCALL                                               'print_r'
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                                 
   86     9        ECHO                                                     '%3C%2Fcode%3E%3C%2Fpre%3E'
         10      > RETURN                                                   1

Class Template:
Function __clone:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 11
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 10
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/bTcrJ
function name:  __clone
number of ops:  13
compiled vars:  !0 = $value, !1 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   FETCH_THIS                                       ~2      
          1      > FE_RESET_R                                       $3      ~2, ->11
          2    > > FE_FETCH_R                                       ~4      $3, !0, ->11
          3    >   ASSIGN                                                   !1, ~4
          4        INSTANCEOF                                               !0
          5      > JMPZ                                                     ~6, ->10
          6    >   FETCH_THIS                                       $7      
          7        CLONE                                            ~9      !0
          8        ASSIGN_DIM                                               $7, !1
          9        OP_DATA                                                  ~9
         10    > > JMP                                                      ->2
         11    >   FE_FREE                                                  $3
   60    12      > RETURN                                                   null

End of function __clone

Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 8
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/bTcrJ
function name:  __construct
number of ops:  10
compiled vars:  !0 = $items, !1 = $value, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   RECV_INIT                                        !0      <array>
   63     1      > FE_RESET_R                                       $3      !0, ->8
          2    > > FE_FETCH_R                                       ~4      $3, !1, ->8
          3    >   ASSIGN                                                   !2, ~4
          4        FETCH_THIS                                       $6      
          5        ASSIGN_DIM                                               $6, !2
          6        OP_DATA                                                  !1
          7      > JMP                                                      ->2
          8    >   FE_FREE                                                  $3
   64     9      > RETURN                                                   null

End of function __construct

Function offsetset:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/bTcrJ
function name:  offsetSet
number of ops:  18
compiled vars:  !0 = $offset, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   67     2        TYPE_CHECK                                  128          !1
          3      > JMPZ                                                     ~2, ->9
          4    >   NEW                          self                $3      
          5        INIT_ARRAY                                       ~4      !1, !0
          6        SEND_VAL_EX                                              ~4
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !1, $3
   68     9    >   TYPE_CHECK                                    2          !0
         10      > JMPZ                                                     ~7, ->15
   69    11    >   FETCH_OBJ_W                                      $8      'implied_key_list'
         12        ASSIGN_DIM                                               $8
         13        OP_DATA                                                  !1
         14      > JMP                                                      ->17
   71    15    >   ASSIGN_OBJ                                               !0
         16        OP_DATA                                                  !1
   73    17    > > RETURN                                                   null

End of function offsetset

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

End of function offsetget

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

End of function offsetexists

Function offsetunset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bTcrJ
function name:  offsetUnset
number of ops:  3
compiled vars:  !0 = $offset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   RECV                                             !0      
          1        UNSET_OBJ                                                !0
          2      > RETURN                                                   null

End of function offsetunset

End of class Template.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.16 ms | 1404 KiB | 15 Q