3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace ArrayAccessAdapter; use ArrayAccess; use IteratorAggregate; use SimpleXmlIterator; use RuntimeException; class XmlArrayAccess implements ArrayAccess, IteratorAggregate { const ATTRIBUTE_PREFIX = '@'; private $xml; public function __construct(SimpleXmlIterator $xml) { $this->xml = $xml; } private function getElement($offset) { $isAttribute = false; if (substr($offset, 0, 1) == self::ATTRIBUTE_PREFIX) { $offset = substr($offset, 1); $isAttribute = false; } if (false !== $this->xml->$offset->asXML() && !$isAttribute) { $xml = $this->xml->$offset; } else { $xml = $this->xml[$offset]; } return $xml; } public function offsetExists($offset) { return !is_null($this->getElement($offset)); } public function offsetGet($offset) { if ($this->xml->getName() == $offset) { return $this; } $xml = $this->getElement($offset); if (is_null($xml)) { throw new RuntimeException('unknown node or attribute "' . $offset . '"'); } return new self($xml); } public function offsetSet($offset, $value) { throw new RuntimeException('This is readonly. writing to "' . (string) $offset . '" with value "' . (string) $value . '" not possible'); } public function offsetUnset($offset) { throw new RuntimeException('This is readonly. Unsetting "' . (string) $offset . '" not possible'); } public function getIterator() { return $this->xml; } public function __toString() { return (string) $this->xml; } } $xml = '<?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading foo="bar">Reminder</heading> <body>Don\'t forget me this weekend!</body> <recursive> <val>2</val> <val>4</val> </recursive> </note>'; $simple = new SimpleXmlIterator($xml); $xml = new XmlArrayAccess($simple); echo $xml['note']['to'][0]; echo $xml['note']['recursive']['val'][1]; echo $xml['note']['heading']['@foo']; echo $xml['note']['heading']['foo'];
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EQK10
function name:  (null)
number of ops:  28
compiled vars:  !0 = $xml, !1 = $simple
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   DECLARE_CLASS                                            'arrayaccessadapter%5Cxmlarrayaccess'
   83     1        ASSIGN                                                   !0, '%3C%3Fxml+version%3D%221.0%22%3F%3E%0A%3Cnote%3E%0A++++%3Cto%3ETove%3C%2Fto%3E%0A++++%3Cfrom%3EJani%3C%2Ffrom%3E%0A++++%3Cheading+foo%3D%22bar%22%3EReminder%3C%2Fheading%3E%0A++++%3Cbody%3EDon%27t+forget+me+this+weekend%21%3C%2Fbody%3E%0A%09%3Crecursive%3E%0A%09%09%3Cval%3E2%3C%2Fval%3E%0A%09%09%3Cval%3E4%3C%2Fval%3E%0A%09%3C%2Frecursive%3E%0A%3C%2Fnote%3E'
   94     2        NEW                                              $3      'SimpleXmlIterator'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $3
   96     6        NEW                                              $6      'ArrayAccessAdapter%5CXmlArrayAccess'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !0, $6
   98    10        FETCH_DIM_R                                      ~9      !0, 'note'
         11        FETCH_DIM_R                                      ~10     ~9, 'to'
         12        FETCH_DIM_R                                      ~11     ~10, 0
         13        ECHO                                                     ~11
   99    14        FETCH_DIM_R                                      ~12     !0, 'note'
         15        FETCH_DIM_R                                      ~13     ~12, 'recursive'
         16        FETCH_DIM_R                                      ~14     ~13, 'val'
         17        FETCH_DIM_R                                      ~15     ~14, 1
         18        ECHO                                                     ~15
  100    19        FETCH_DIM_R                                      ~16     !0, 'note'
         20        FETCH_DIM_R                                      ~17     ~16, 'heading'
         21        FETCH_DIM_R                                      ~18     ~17, '%40foo'
         22        ECHO                                                     ~18
  101    23        FETCH_DIM_R                                      ~19     !0, 'note'
         24        FETCH_DIM_R                                      ~20     ~19, 'heading'
         25        FETCH_DIM_R                                      ~21     ~20, 'foo'
         26        ECHO                                                     ~21
         27      > RETURN                                                   1

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

End of function __construct

Function getelement:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 16
Branch analysis from position: 10
2 jumps found. (Code = 46) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 29
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
Branch analysis from position: 16
filename:       /in/EQK10
function name:  getElement
number of ops:  34
compiled vars:  !0 = $offset, !1 = $isAttribute, !2 = $xml
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   23     1        ASSIGN                                                   !1, <false>
   24     2        INIT_NS_FCALL_BY_NAME                                    'ArrayAccessAdapter%5Csubstr'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAL_EX                                              0
          5        SEND_VAL_EX                                              1
          6        DO_FCALL                                      0  $4      
          7        FETCH_CLASS_CONSTANT                             ~5      'ATTRIBUTE_PREFIX'
          8        IS_EQUAL                                                 $4, ~5
          9      > JMPZ                                                     ~6, ->16
   25    10    >   INIT_NS_FCALL_BY_NAME                                    'ArrayAccessAdapter%5Csubstr'
         11        SEND_VAR_EX                                              !0
         12        SEND_VAL_EX                                              1
         13        DO_FCALL                                      0  $7      
         14        ASSIGN                                                   !0, $7
   26    15        ASSIGN                                                   !1, <false>
   29    16    >   FETCH_OBJ_R                                      ~10     'xml'
         17        FETCH_OBJ_R                                      ~11     ~10, !0
         18        INIT_METHOD_CALL                                         ~11, 'asXML'
         19        DO_FCALL                                      0  $12     
         20        TYPE_CHECK                                  1018  ~13     $12
         21      > JMPZ_EX                                          ~13     ~13, ->24
         22    >   BOOL_NOT                                         ~14     !1
         23        BOOL                                             ~13     ~14
         24    > > JMPZ                                                     ~13, ->29
   30    25    >   FETCH_OBJ_R                                      ~15     'xml'
         26        FETCH_OBJ_R                                      ~16     ~15, !0
         27        ASSIGN                                                   !2, ~16
         28      > JMP                                                      ->32
   33    29    >   FETCH_OBJ_R                                      ~18     'xml'
         30        FETCH_DIM_R                                      ~19     ~18, !0
         31        ASSIGN                                                   !2, ~19
   36    32    > > RETURN                                                   !2
   37    33*     > RETURN                                                   null

End of function getelement

Function offsetexists:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EQK10
function name:  offsetExists
number of ops:  10
compiled vars:  !0 = $offset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
   41     1        INIT_NS_FCALL_BY_NAME                                    'ArrayAccessAdapter%5Cis_null'
          2        INIT_METHOD_CALL                                         'getElement'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $1      
          5        SEND_VAR_NO_REF_EX                                       $1
          6        DO_FCALL                                      0  $2      
          7        BOOL_NOT                                         ~3      $2
          8      > RETURN                                                   ~3
   42     9*     > RETURN                                                   null

End of function offsetexists

Function offsetget:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 22
Branch analysis from position: 16
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EQK10
function name:  offsetGet
number of ops:  27
compiled vars:  !0 = $offset, !1 = $xml
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   46     1        FETCH_OBJ_R                                      ~2      'xml'
          2        INIT_METHOD_CALL                                         ~2, 'getName'
          3        DO_FCALL                                      0  $3      
          4        IS_EQUAL                                                 !0, $3
          5      > JMPZ                                                     ~4, ->8
   47     6    >   FETCH_THIS                                       ~5      
          7      > RETURN                                                   ~5
   50     8    >   INIT_METHOD_CALL                                         'getElement'
          9        SEND_VAR                                                 !0
         10        DO_FCALL                                      0  $6      
         11        ASSIGN                                                   !1, $6
   52    12        INIT_NS_FCALL_BY_NAME                                    'ArrayAccessAdapter%5Cis_null'
         13        SEND_VAR_EX                                              !1
         14        DO_FCALL                                      0  $8      
         15      > JMPZ                                                     $8, ->22
   53    16    >   NEW                                              $9      'RuntimeException'
         17        CONCAT                                           ~10     'unknown+node+or+attribute+%22', !0
         18        CONCAT                                           ~11     ~10, '%22'
         19        SEND_VAL_EX                                              ~11
         20        DO_FCALL                                      0          
         21      > THROW                                         0          $9
   56    22    >   NEW                          self                $13     
         23        SEND_VAR_EX                                              !1
         24        DO_FCALL                                      0          
         25      > RETURN                                                   $13
   57    26*     > RETURN                                                   null

End of function offsetget

Function offsetset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/EQK10
function name:  offsetSet
number of ops:  13
compiled vars:  !0 = $offset, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   61     2        NEW                                              $2      'RuntimeException'
          3        CAST                                          6  ~3      !0
          4        CONCAT                                           ~4      'This+is+readonly.+writing+to+%22', ~3
          5        CONCAT                                           ~5      ~4, '%22+with+value+%22'
          6        CAST                                          6  ~6      !1
          7        CONCAT                                           ~7      ~5, ~6
          8        CONCAT                                           ~8      ~7, '%22+not+possible'
          9        SEND_VAL_EX                                              ~8
         10        DO_FCALL                                      0          
         11      > THROW                                         0          $2
   62    12*     > RETURN                                                   null

End of function offsetset

Function offsetunset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/EQK10
function name:  offsetUnset
number of ops:  9
compiled vars:  !0 = $offset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   RECV                                             !0      
   66     1        NEW                                              $1      'RuntimeException'
          2        CAST                                          6  ~2      !0
          3        CONCAT                                           ~3      'This+is+readonly.+Unsetting+%22', ~2
          4        CONCAT                                           ~4      ~3, '%22+not+possible'
          5        SEND_VAL_EX                                              ~4
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $1
   67     8*     > RETURN                                                   null

End of function offsetunset

Function getiterator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EQK10
function name:  getIterator
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   FETCH_OBJ_R                                      ~0      'xml'
          1      > RETURN                                                   ~0
   72     2*     > RETURN                                                   null

End of function getiterator

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EQK10
function name:  __toString
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E >   FETCH_OBJ_R                                      ~0      'xml'
          1        CAST                                          6  ~1      ~0
          2        VERIFY_RETURN_TYPE                                       ~1
          3      > RETURN                                                   ~1
   77     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function __tostring

End of class ArrayAccessAdapter\XmlArrayAccess.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.65 ms | 1408 KiB | 17 Q