3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A implements JsonSerializable { protected $a = array(); public function __construct() { $this->a = array( new B, new B ); } public function jsonSerialize() { return $this->a; } } class B implements JsonSerializable { protected $b = array( 'foo' => 'bar' ); public function jsonSerialize() { return $this->b; } } $foo = new A(); $json = json_encode($foo); var_dump($json); class Example implements JsonSerializable { private $_data = array(); public function getData($key = null) { if (is_null($key)) { return $this->_data; } if (array_key_exists($key, $this->_data)) { return $this->_data[$key]; } return null; } public function setData($key, $value = null) { if (is_array($key)) { if (!is_null($value)) { throw new Exception('When setting data in bulk, the value argument must be null'); } $this->_data = $key; return $this; } $this->_data[(string)$key] = $value; return $this; } public function jsonSerialize() { return $this->_data; } } $example = new Example(); $example->setData(array( 'first' => 1, 'second' => 2, ))->setData('third', 3); var_dump(json_encode($example)); $example2 = new Example(); $example3 = new Example(); $example2->setData(array( 'banana' => 'yellow', )); $example3->setData(array( 'apple' => 'red', )); $arr = array( $example, $example2, $example3, ); var_dump(json_encode($arr));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5A0i0
function name:  (null)
number of ops:  52
compiled vars:  !0 = $foo, !1 = $json, !2 = $example, !3 = $example2, !4 = $example3, !5 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                            'a'
   18     1        DECLARE_CLASS                                            'b'
   29     2        NEW                                              $6      'A'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $6
   31     5        INIT_FCALL                                               'json_encode'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $9      
          8        ASSIGN                                                   !1, $9
   33     9        INIT_FCALL                                               'var_dump'
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                                 
   35    12        DECLARE_CLASS                                            'example'
   75    13        NEW                                              $12     'Example'
         14        DO_FCALL                                      0          
         15        ASSIGN                                                   !2, $12
   76    16        INIT_METHOD_CALL                                         !2, 'setData'
   77    17        SEND_VAL_EX                                              <array>
         18        DO_FCALL                                      0  $15     
   79    19        INIT_METHOD_CALL                                         $15, 'setData'
         20        SEND_VAL_EX                                              'third'
         21        SEND_VAL_EX                                              3
         22        DO_FCALL                                      0          
   81    23        INIT_FCALL                                               'var_dump'
         24        INIT_FCALL                                               'json_encode'
         25        SEND_VAR                                                 !2
         26        DO_ICALL                                         $17     
         27        SEND_VAR                                                 $17
         28        DO_ICALL                                                 
   83    29        NEW                                              $19     'Example'
         30        DO_FCALL                                      0          
         31        ASSIGN                                                   !3, $19
   84    32        NEW                                              $22     'Example'
         33        DO_FCALL                                      0          
         34        ASSIGN                                                   !4, $22
   86    35        INIT_METHOD_CALL                                         !3, 'setData'
   87    36        SEND_VAL_EX                                              <array>
         37        DO_FCALL                                      0          
   89    38        INIT_METHOD_CALL                                         !4, 'setData'
   90    39        SEND_VAL_EX                                              <array>
         40        DO_FCALL                                      0          
   94    41        INIT_ARRAY                                       ~27     !2
   95    42        ADD_ARRAY_ELEMENT                                ~27     !3
   96    43        ADD_ARRAY_ELEMENT                                ~27     !4
   93    44        ASSIGN                                                   !5, ~27
   99    45        INIT_FCALL                                               'var_dump'
         46        INIT_FCALL                                               'json_encode'
         47        SEND_VAR                                                 !5
         48        DO_ICALL                                         $29     
         49        SEND_VAR                                                 $29
         50        DO_ICALL                                                 
         51      > RETURN                                                   1

Class A:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5A0i0
function name:  __construct
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   NEW                                              $1      'B'
          1        DO_FCALL                                      0          
          2        INIT_ARRAY                                       ~3      $1
          3        NEW                                              $4      'B'
          4        DO_FCALL                                      0          
          5        ADD_ARRAY_ELEMENT                                ~3      $4
          6        ASSIGN_OBJ                                               'a'
          7        OP_DATA                                                  ~3
   10     8      > RETURN                                                   null

End of function __construct

Function jsonserialize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5A0i0
function name:  jsonSerialize
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   FETCH_OBJ_R                                      ~0      'a'
          1      > RETURN                                                   ~0
   15     2*     > RETURN                                                   null

End of function jsonserialize

End of class A.

Class B:
Function jsonserialize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5A0i0
function name:  jsonSerialize
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   FETCH_OBJ_R                                      ~0      'b'
          1      > RETURN                                                   ~0
   25     2*     > RETURN                                                   null

End of function jsonserialize

End of class B.

Class Example:
Function getdata:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 11
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5A0i0
function name:  getData
number of ops:  13
compiled vars:  !0 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV_INIT                                        !0      null
   41     1        TYPE_CHECK                                    2          !0
          2      > JMPZ                                                     ~1, ->5
   42     3    >   FETCH_OBJ_R                                      ~2      '_data'
          4      > RETURN                                                   ~2
   45     5    >   FETCH_OBJ_R                                      ~3      '_data'
          6        ARRAY_KEY_EXISTS                                         !0, ~3
          7      > JMPZ                                                     ~4, ->11
   46     8    >   FETCH_OBJ_R                                      ~5      '_data'
          9        FETCH_DIM_R                                      ~6      ~5, !0
         10      > RETURN                                                   ~6
   49    11    > > RETURN                                                   null
   50    12*     > RETURN                                                   null

End of function getdata

Function setdata:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 15
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5A0i0
function name:  setData
number of ops:  22
compiled vars:  !0 = $key, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   54     2        TYPE_CHECK                                  128          !0
          3      > JMPZ                                                     ~2, ->15
   55     4    >   TYPE_CHECK                                    2  ~3      !1
          5        BOOL_NOT                                         ~4      ~3
          6      > JMPZ                                                     ~4, ->11
   56     7    >   NEW                                              $5      'Exception'
          8        SEND_VAL_EX                                              'When+setting+data+in+bulk%2C+the+value+argument+must+be+null'
          9        DO_FCALL                                      0          
         10      > THROW                                         0          $5
   59    11    >   ASSIGN_OBJ                                               '_data'
         12        OP_DATA                                                  !0
   61    13        FETCH_THIS                                       ~8      
         14      > RETURN                                                   ~8
   64    15    >   CAST                                          6  ~10     !0
         16        FETCH_OBJ_W                                      $9      '_data'
         17        ASSIGN_DIM                                               $9, ~10
         18        OP_DATA                                                  !1
   66    19        FETCH_THIS                                       ~12     
         20      > RETURN                                                   ~12
   67    21*     > RETURN                                                   null

End of function setdata

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

End of function jsonserialize

End of class Example.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.44 ms | 1408 KiB | 17 Q