3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ObjectAndXML { private static $xml; // Constructor public function __construct() { $this->xml = new XmlWriter(); $this->xml->openMemory(); $this->xml->startDocument('1.0'); $this->xml->setIndent(true); } // Method to convert Object into XML string public function objToXML($obj) { $this->getObject2XML($this->xml, $obj); $this->xml->endElement(); return $this->xml->outputMemory(true); } // Method to convert XML string into Object public function xmlToObj($xmlString) { return simplexml_load_string($xmlString); } private function getObject2XML(XMLWriter $xml, $data) { foreach($data as $key => $value) { if(is_object($value)) { $xml->startElement($key); $this->getObject2XML($xml, $value); $xml->endElement(); continue; } else if(is_array($value)) { $this->getArray2XML($xml, $key, $value); } if (is_string($value)) { $xml->writeElement($key, $value); } } } private function getArray2XML(XMLWriter $xml, $keyParent, $data) { foreach($data as $key => $value) { if (is_string($value)) { $xml->writeElement($keyParent, $value); continue; } if (is_numeric($key)) { $xml->startElement($keyParent); } if(is_object($value)) { $this->getObject2XML($xml, $value); } else if(is_array($value)) { $this->getArray2XML($xml, $key, $value); continue; } if (is_numeric($key)) { $xml->endElement(); } } } } $obj = new ObjectAndXML(); $str = <<<STR <?xml version="1.0" encoding="utf-8"?> <records> <person> <name>XYZ</name> <age>28</age> <gender>Male</gender> </person> <person> <name>ABC</name> <age>25</age> <gender>Male</gender> </person> </records> STR; $recordsObj = $obj->xmlToObj($str); echo '<pre>'; var_dump($recordsObj);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sbU2K
function name:  (null)
number of ops:  13
compiled vars:  !0 = $obj, !1 = $str, !2 = $recordsObj
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   NEW                                              $3      'ObjectAndXML'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $3
   74     3        ASSIGN                                                   !1, '%3C%3Fxml+version%3D%221.0%22+encoding%3D%22utf-8%22%3F%3E%0A%3Crecords%3E%0A++++%3Cperson%3E%0A++++%09%3Cname%3EXYZ%3C%2Fname%3E%0A++++++++%3Cage%3E28%3C%2Fage%3E%0A++++++++%3Cgender%3EMale%3C%2Fgender%3E%0A++++%3C%2Fperson%3E%0A++++%3Cperson%3E%0A++++%09%3Cname%3EABC%3C%2Fname%3E%0A++++++++%3Cage%3E25%3C%2Fage%3E%0A++++++++%3Cgender%3EMale%3C%2Fgender%3E%0A++++%3C%2Fperson%3E%0A%3C%2Frecords%3E'
   90     4        INIT_METHOD_CALL                                         !0, 'xmlToObj'
          5        SEND_VAR_EX                                              !1
          6        DO_FCALL                                      0  $7      
          7        ASSIGN                                                   !2, $7
   91     8        ECHO                                                     '%3Cpre%3E'
   92     9        INIT_FCALL                                               'var_dump'
         10        SEND_VAR                                                 !2
         11        DO_ICALL                                                 
         12      > RETURN                                                   1

Class ObjectAndXML:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sbU2K
function name:  __construct
number of ops:  16
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   NEW                                              $1      'XmlWriter'
          1        DO_FCALL                                      0          
          2        ASSIGN_OBJ                                               'xml'
          3        OP_DATA                                                  $1
    9     4        FETCH_OBJ_R                                      ~3      'xml'
          5        INIT_METHOD_CALL                                         ~3, 'openMemory'
          6        DO_FCALL                                      0          
   10     7        FETCH_OBJ_R                                      ~5      'xml'
          8        INIT_METHOD_CALL                                         ~5, 'startDocument'
          9        SEND_VAL_EX                                              '1.0'
         10        DO_FCALL                                      0          
   11    11        FETCH_OBJ_R                                      ~7      'xml'
         12        INIT_METHOD_CALL                                         ~7, 'setIndent'
         13        SEND_VAL_EX                                              <true>
         14        DO_FCALL                                      0          
   12    15      > RETURN                                                   null

End of function __construct

Function objtoxml:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sbU2K
function name:  objToXML
number of ops:  16
compiled vars:  !0 = $obj
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   16     1        INIT_METHOD_CALL                                         'getObject2XML'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_OBJ_FUNC_ARG                               $1      'xml'
          4        SEND_FUNC_ARG                                            $1
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
   18     7        FETCH_OBJ_R                                      ~3      'xml'
          8        INIT_METHOD_CALL                                         ~3, 'endElement'
          9        DO_FCALL                                      0          
   20    10        FETCH_OBJ_R                                      ~5      'xml'
         11        INIT_METHOD_CALL                                         ~5, 'outputMemory'
         12        SEND_VAL_EX                                              <true>
         13        DO_FCALL                                      0  $6      
         14      > RETURN                                                   $6
   21    15*     > RETURN                                                   null

End of function objtoxml

Function xmltoobj:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sbU2K
function name:  xmlToObj
number of ops:  6
compiled vars:  !0 = $xmlString
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        INIT_FCALL                                               'simplexml_load_string'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4      > RETURN                                                   $1
   26     5*     > RETURN                                                   null

End of function xmltoobj

Function getobject2xml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 32
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 32
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 18
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 25
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 31
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 31
Branch analysis from position: 25
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/sbU2K
function name:  getObject2XML
number of ops:  34
compiled vars:  !0 = $xml, !1 = $data, !2 = $value, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   29     2      > FE_RESET_R                                       $4      !1, ->32
          3    > > FE_FETCH_R                                       ~5      $4, !2, ->32
          4    >   ASSIGN                                                   !3, ~5
   30     5        TYPE_CHECK                                  256          !2
          6      > JMPZ                                                     ~7, ->18
   31     7    >   INIT_METHOD_CALL                                         !0, 'startElement'
          8        SEND_VAR_EX                                              !3
          9        DO_FCALL                                      0          
   32    10        INIT_METHOD_CALL                                         'getObject2XML'
         11        SEND_VAR                                                 !0
         12        SEND_VAR                                                 !2
         13        DO_FCALL                                      0          
   33    14        INIT_METHOD_CALL                                         !0, 'endElement'
         15        DO_FCALL                                      0          
   34    16      > JMP                                                      ->3
         17*       JMP                                                      ->25
   36    18    >   TYPE_CHECK                                  128          !2
         19      > JMPZ                                                     ~11, ->25
   37    20    >   INIT_METHOD_CALL                                         'getArray2XML'
         21        SEND_VAR_EX                                              !0
         22        SEND_VAR_EX                                              !3
         23        SEND_VAR_EX                                              !2
         24        DO_FCALL                                      0          
   40    25    >   TYPE_CHECK                                   64          !2
         26      > JMPZ                                                     ~13, ->31
   41    27    >   INIT_METHOD_CALL                                         !0, 'writeElement'
         28        SEND_VAR_EX                                              !3
         29        SEND_VAR_EX                                              !2
         30        DO_FCALL                                      0          
   29    31    > > JMP                                                      ->3
         32    >   FE_FREE                                                  $4
   44    33      > RETURN                                                   null

End of function getobject2xml

Function getarray2xml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 42
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 42
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 20
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 27
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 41
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 41
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 35
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 35
Branch analysis from position: 20
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
filename:       /in/sbU2K
function name:  getArray2XML
number of ops:  44
compiled vars:  !0 = $xml, !1 = $keyParent, !2 = $data, !3 = $value, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   47     3      > FE_RESET_R                                       $5      !2, ->42
          4    > > FE_FETCH_R                                       ~6      $5, !3, ->42
          5    >   ASSIGN                                                   !4, ~6
   48     6        TYPE_CHECK                                   64          !3
          7      > JMPZ                                                     ~8, ->13
   49     8    >   INIT_METHOD_CALL                                         !0, 'writeElement'
          9        SEND_VAR_EX                                              !1
         10        SEND_VAR_EX                                              !3
         11        DO_FCALL                                      0          
   50    12      > JMP                                                      ->4
   53    13    >   INIT_FCALL                                               'is_numeric'
         14        SEND_VAR                                                 !4
         15        DO_ICALL                                         $10     
         16      > JMPZ                                                     $10, ->20
   54    17    >   INIT_METHOD_CALL                                         !0, 'startElement'
         18        SEND_VAR_EX                                              !1
         19        DO_FCALL                                      0          
   57    20    >   TYPE_CHECK                                  256          !3
         21      > JMPZ                                                     ~12, ->27
   58    22    >   INIT_METHOD_CALL                                         'getObject2XML'
         23        SEND_VAR                                                 !0
         24        SEND_VAR                                                 !3
         25        DO_FCALL                                      0          
         26      > JMP                                                      ->35
   60    27    >   TYPE_CHECK                                  128          !3
         28      > JMPZ                                                     ~14, ->35
   61    29    >   INIT_METHOD_CALL                                         'getArray2XML'
         30        SEND_VAR                                                 !0
         31        SEND_VAR                                                 !4
         32        SEND_VAR                                                 !3
         33        DO_FCALL                                      0          
   62    34      > JMP                                                      ->4
   65    35    >   INIT_FCALL                                               'is_numeric'
         36        SEND_VAR                                                 !4
         37        DO_ICALL                                         $16     
         38      > JMPZ                                                     $16, ->41
   66    39    >   INIT_METHOD_CALL                                         !0, 'endElement'
         40        DO_FCALL                                      0          
   47    41    > > JMP                                                      ->4
         42    >   FE_FREE                                                  $5
   69    43      > RETURN                                                   null

End of function getarray2xml

End of class ObjectAndXML.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
166.72 ms | 1408 KiB | 19 Q