3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Example of dynamic property that is initialized only when the property is invoked, * i.e. only on get. */ class DynamicPropInitOnGet { public $prop1; public $prop2; public $prop3; public function __construct( $props ) { foreach ( $props as $prop => $value ) { $this->$prop = $value; } } public function __get( $key ) { switch ( $key ) { case 'known': $known = new stdClass(); foreach ( array( 'prop1', 'prop2' ) as $column ) { $known->{$column} = isset( $this->{$column} ) ? $this->{$column} : null; } return $known; } } } class TestMagicMethods { private $obj; private $is_known_prop = false; public function __construct( $obj ) { $this->obj = $obj; } public function test( $prop ) { $this->is_known_prop = ( 'known' === $prop ); // __isset. $this->test_isset( $prop ); // __get(). $this->test_get( $prop ); // __isset() after __get(). $this->test_isset( $prop, true ); // __unset(). $this->test_unset( $prop ); } private function test_isset( $prop, $after_get = false ) { $actual = isset( $this->obj->$prop ); if ( ! $after_get ) { printf( "__isset() results should be false %s\n", $this->test_results( false === $actual ) ); } else { $expected = $this->is_known_prop; printf( "\n\n__isset() after __get() results should be %s %s\n", $this->is_known_prop ? 'true' : 'false', $this->test_results( $expected == $actual ) ); } var_dump( $actual ); } private function test_get( $prop ) { $actual = $this->obj->$prop; $message = "\n\n__get() results should be %s %s\n"; if ( $this->is_known_prop ) { printf( $message, 'an object with prop1 and prop2', $this->test_results( $actual instanceof stdClass ) ); } else { printf( $message, 'null', $this->test_results( null === $actual ) ); } var_dump( $actual ); } private function test_set( $prop ) { $value = $this->is_known_prop ? (array) $this->$obj->known : 'I am an unknown dynamic property'; $obj->$prop = $value; $actual = $this->obj->$prop; if ( $this->is_known_prop ) { printf( "\n\n" . '__set() results should set the value %s and not throw a deprecation on PHP 8.2+.' . 'However, once the __get() is called again, it will set reset it to an object with prop1 and prop2.' . "\n", $this->test_results( $actual instanceof stdClass ) ); } else { printf( "\n\n" . '__set() results should set the value %s and not throw a deprecation on PHP 8.2+.' . 'The set value should remain set when invoking __get()' . "\n", $this->test_results( $value === $actual ) ); } var_dump( $actual ); } private function test_unset( $prop ) {// unset. unset( $this->obj->$prop ); $actual = $this->obj->$prop; $message = "\n\n__unset() + __get() results should be %s %s\n"; if ( $this->is_known_prop ) { printf( $message, 're-init the object with prop1 and prop2', $this->test_results( $actual instanceof stdClass ) ); } else { printf( $message, 'null', $this->test_results( null === $actual ) ); } var_dump( $actual ); } private function test_results( $actual ) { return $actual ? '✅' : '❌'; } } $obj = new DynamicPropInitOnGet( array( 'prop1' => 'foo', 'prop2' => 'bar', 'prop3' => 'baz', ) ); $tester = new TestMagicMethods( $obj ); echo "***** Test the 'known' property *****\n\n"; $tester->test( 'known' ); echo "\n\n***** Unknown, unexpected dynamic property *****\n\n"; $tester->test( 'unknown' );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VGDNn
function name:  (null)
number of ops:  17
compiled vars:  !0 = $obj, !1 = $tester
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  138     0  E >   NEW                                              $2      'DynamicPropInitOnGet'
  140     1        SEND_VAL_EX                                              <array>
  138     2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $2
  146     4        NEW                                              $5      'TestMagicMethods'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !1, $5
  148     8        ECHO                                                     '%2A%2A%2A%2A%2A+Test+the+%27known%27+property+%2A%2A%2A%2A%2A%0A%0A'
  149     9        INIT_METHOD_CALL                                         !1, 'test'
         10        SEND_VAL_EX                                              'known'
         11        DO_FCALL                                      0          
  151    12        ECHO                                                     '%0A%0A%2A%2A%2A%2A%2A+Unknown%2C+unexpected+dynamic+property+%2A%2A%2A%2A%2A%0A%0A'
  152    13        INIT_METHOD_CALL                                         !1, 'test'
         14        SEND_VAL_EX                                              'unknown'
         15        DO_FCALL                                      0          
         16      > RETURN                                                   1

Class DynamicPropInitOnGet:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 7
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/VGDNn
function name:  __construct
number of ops:  9
compiled vars:  !0 = $props, !1 = $value, !2 = $prop
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   14     1      > FE_RESET_R                                       $3      !0, ->7
          2    > > FE_FETCH_R                                       ~4      $3, !1, ->7
          3    >   ASSIGN                                                   !2, ~4
   15     4        ASSIGN_OBJ                                               !2
          5        OP_DATA                                                  !1
   14     6      > JMP                                                      ->2
          7    >   FE_FREE                                                  $3
   17     8      > RETURN                                                   null

End of function __construct

Function __get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 18
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 18
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 14
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/VGDNn
function name:  __get
number of ops:  21
compiled vars:  !0 = $key, !1 = $known, !2 = $column
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   21     1        IS_EQUAL                                                 !0, 'known'
          2      > JMPNZ                                                    ~3, ->4
          3    > > JMP                                                      ->20
   22     4    >   NEW                                              $4      'stdClass'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !1, $4
   23     7      > FE_RESET_R                                       $7      <array>, ->18
          8    > > FE_FETCH_R                                               $7, !2, ->18
   24     9    >   ISSET_ISEMPTY_PROP_OBJ                                   !2
         10      > JMPZ                                                     ~9, ->14
         11    >   FETCH_OBJ_R                                      ~10     !2
         12        QM_ASSIGN                                        ~11     ~10
         13      > JMP                                                      ->15
         14    >   QM_ASSIGN                                        ~11     null
         15    >   ASSIGN_OBJ                                               !1, !2
         16        OP_DATA                                                  ~11
   23    17      > JMP                                                      ->8
         18    >   FE_FREE                                                  $7
   27    19      > RETURN                                                   !1
   29    20    > > RETURN                                                   null

End of function __get

End of class DynamicPropInitOnGet.

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

End of function __construct

Function test:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VGDNn
function name:  test
number of ops:  18
compiled vars:  !0 = $prop
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   41     1        IS_IDENTICAL                                     ~2      !0, 'known'
          2        ASSIGN_OBJ                                               'is_known_prop'
          3        OP_DATA                                                  ~2
   45     4        INIT_METHOD_CALL                                         'test_isset'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
   48     7        INIT_METHOD_CALL                                         'test_get'
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0          
   51    10        INIT_METHOD_CALL                                         'test_isset'
         11        SEND_VAR_EX                                              !0
         12        SEND_VAL_EX                                              <true>
         13        DO_FCALL                                      0          
   54    14        INIT_METHOD_CALL                                         'test_unset'
         15        SEND_VAR_EX                                              !0
         16        DO_FCALL                                      0          
   55    17      > RETURN                                                   null

End of function test

Function test_isset:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 16
Branch analysis from position: 7
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: 16
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VGDNn
function name:  test_isset
number of ops:  36
compiled vars:  !0 = $prop, !1 = $after_get, !2 = $actual, !3 = $expected
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
   58     2        FETCH_OBJ_IS                                     ~4      'obj'
          3        ISSET_ISEMPTY_PROP_OBJ                           ~5      ~4, !0
          4        ASSIGN                                                   !2, ~5
   60     5        BOOL_NOT                                         ~7      !1
          6      > JMPZ                                                     ~7, ->16
   61     7    >   INIT_FCALL                                               'printf'
          8        SEND_VAL                                                 '__isset%28%29+results+should+be+false+%25s%0A'
          9        INIT_METHOD_CALL                                         'test_results'
         10        TYPE_CHECK                                    4  ~8      !2
         11        SEND_VAL_EX                                              ~8
         12        DO_FCALL                                      0  $9      
         13        SEND_VAR                                                 $9
         14        DO_ICALL                                                 
   60    15      > JMP                                                      ->32
   63    16    >   FETCH_OBJ_R                                      ~11     'is_known_prop'
         17        ASSIGN                                                   !3, ~11
   64    18        INIT_FCALL                                               'printf'
   65    19        SEND_VAL                                                 '%0A%0A__isset%28%29+after+__get%28%29+results+should+be+%25s+%25s%0A'
   66    20        FETCH_OBJ_R                                      ~13     'is_known_prop'
         21      > JMPZ                                                     ~13, ->24
         22    >   QM_ASSIGN                                        ~14     'true'
         23      > JMP                                                      ->25
         24    >   QM_ASSIGN                                        ~14     'false'
         25    >   SEND_VAL                                                 ~14
   67    26        INIT_METHOD_CALL                                         'test_results'
         27        IS_EQUAL                                         ~15     !3, !2
         28        SEND_VAL_EX                                              ~15
         29        DO_FCALL                                      0  $16     
         30        SEND_VAR                                                 $16
   64    31        DO_ICALL                                                 
   71    32    >   INIT_FCALL                                               'var_dump'
         33        SEND_VAR                                                 !2
         34        DO_ICALL                                                 
   72    35      > RETURN                                                   null

End of function test_isset

Function test_get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 17
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VGDNn
function name:  test_get
number of ops:  30
compiled vars:  !0 = $prop, !1 = $actual, !2 = $message
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   RECV                                             !0      
   75     1        FETCH_OBJ_R                                      ~3      'obj'
          2        FETCH_OBJ_R                                      ~4      ~3, !0
          3        ASSIGN                                                   !1, ~4
   76     4        ASSIGN                                                   !2, '%0A%0A__get%28%29+results+should+be+%25s+%25s%0A'
   77     5        FETCH_OBJ_R                                      ~7      'is_known_prop'
          6      > JMPZ                                                     ~7, ->17
   78     7    >   INIT_FCALL                                               'printf'
          8        SEND_VAR                                                 !2
          9        SEND_VAL                                                 'an+object+with+prop1+and+prop2'
         10        INIT_METHOD_CALL                                         'test_results'
         11        INSTANCEOF                                       ~8      !1, 'stdClass'
         12        SEND_VAL_EX                                              ~8
         13        DO_FCALL                                      0  $9      
         14        SEND_VAR                                                 $9
         15        DO_ICALL                                                 
   77    16      > JMP                                                      ->26
   80    17    >   INIT_FCALL                                               'printf'
         18        SEND_VAR                                                 !2
         19        SEND_VAL                                                 'null'
         20        INIT_METHOD_CALL                                         'test_results'
         21        TYPE_CHECK                                    2  ~11     !1
         22        SEND_VAL_EX                                              ~11
         23        DO_FCALL                                      0  $12     
         24        SEND_VAR                                                 $12
         25        DO_ICALL                                                 
   83    26    >   INIT_FCALL                                               'var_dump'
         27        SEND_VAR                                                 !1
         28        DO_ICALL                                                 
   84    29      > RETURN                                                   null

End of function test_get

Function test_set:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 26
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 26
Branch analysis from position: 17
Branch analysis from position: 26
filename:       /in/VGDNn
function name:  test_set
number of ops:  38
compiled vars:  !0 = $prop, !1 = $value, !2 = $obj, !3 = $actual
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   RECV                                             !0      
   87     1        FETCH_OBJ_R                                      ~4      'is_known_prop'
          2      > JMPZ                                                     ~4, ->8
          3    >   FETCH_OBJ_R                                      ~5      !2
          4        FETCH_OBJ_R                                      ~6      ~5, 'known'
          5        CAST                                          7  ~7      ~6
          6        QM_ASSIGN                                        ~8      ~7
          7      > JMP                                                      ->9
          8    >   QM_ASSIGN                                        ~8      'I+am+an+unknown+dynamic+property'
          9    >   ASSIGN                                                   !1, ~8
   88    10        ASSIGN_OBJ                                               !2, !0
         11        OP_DATA                                                  !1
   89    12        FETCH_OBJ_R                                      ~11     'obj'
         13        FETCH_OBJ_R                                      ~12     ~11, !0
         14        ASSIGN                                                   !3, ~12
   91    15        FETCH_OBJ_R                                      ~14     'is_known_prop'
         16      > JMPZ                                                     ~14, ->26
   92    17    >   INIT_FCALL                                               'printf'
   96    18        SEND_VAL                                                 '%0A%0A__set%28%29+results+should+set+the+value+%25s+and+not+throw+a+deprecation+on+PHP+8.2%2B.However%2C+once+the+__get%28%29+is+called+again%2C+it+will+set+reset+it+to+an+object+with+prop1+and+prop2.%0A'
   97    19        INIT_METHOD_CALL                                         'test_results'
         20        INSTANCEOF                                       ~15     !3, 'stdClass'
         21        SEND_VAL_EX                                              ~15
         22        DO_FCALL                                      0  $16     
         23        SEND_VAR                                                 $16
   92    24        DO_ICALL                                                 
   91    25      > JMP                                                      ->34
  100    26    >   INIT_FCALL                                               'printf'
  104    27        SEND_VAL                                                 '%0A%0A__set%28%29+results+should+set+the+value+%25s+and+not+throw+a+deprecation+on+PHP+8.2%2B.The+set+value+should+remain+set+when+invoking+__get%28%29%0A'
  105    28        INIT_METHOD_CALL                                         'test_results'
         29        IS_IDENTICAL                                     ~18     !1, !3
         30        SEND_VAL_EX                                              ~18
         31        DO_FCALL                                      0  $19     
         32        SEND_VAR                                                 $19
  100    33        DO_ICALL                                                 
  109    34    >   INIT_FCALL                                               'var_dump'
         35        SEND_VAR                                                 !3
         36        DO_ICALL                                                 
  110    37      > RETURN                                                   null

End of function test_set

Function test_unset:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 19
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VGDNn
function name:  test_unset
number of ops:  32
compiled vars:  !0 = $prop, !1 = $actual, !2 = $message
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  112     0  E >   RECV                                             !0      
  113     1        FETCH_OBJ_UNSET                                  $3      'obj'
          2        UNSET_OBJ                                                $3, !0
  114     3        FETCH_OBJ_R                                      ~4      'obj'
          4        FETCH_OBJ_R                                      ~5      ~4, !0
          5        ASSIGN                                                   !1, ~5
  116     6        ASSIGN                                                   !2, '%0A%0A__unset%28%29+%2B+__get%28%29+results+should+be+%25s+%25s%0A'
  117     7        FETCH_OBJ_R                                      ~8      'is_known_prop'
          8      > JMPZ                                                     ~8, ->19
  118     9    >   INIT_FCALL                                               'printf'
  119    10        SEND_VAR                                                 !2
  120    11        SEND_VAL                                                 're-init+the+object+with+prop1+and+prop2'
  121    12        INIT_METHOD_CALL                                         'test_results'
         13        INSTANCEOF                                       ~9      !1, 'stdClass'
         14        SEND_VAL_EX                                              ~9
         15        DO_FCALL                                      0  $10     
         16        SEND_VAR                                                 $10
  118    17        DO_ICALL                                                 
  117    18      > JMP                                                      ->28
  124    19    >   INIT_FCALL                                               'printf'
  125    20        SEND_VAR                                                 !2
  126    21        SEND_VAL                                                 'null'
  127    22        INIT_METHOD_CALL                                         'test_results'
         23        TYPE_CHECK                                    2  ~12     !1
         24        SEND_VAL_EX                                              ~12
         25        DO_FCALL                                      0  $13     
         26        SEND_VAR                                                 $13
  124    27        DO_ICALL                                                 
  130    28    >   INIT_FCALL                                               'var_dump'
         29        SEND_VAR                                                 !1
         30        DO_ICALL                                                 
  131    31      > RETURN                                                   null

End of function test_unset

Function test_results:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 4
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VGDNn
function name:  test_results
number of ops:  7
compiled vars:  !0 = $actual
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  133     0  E >   RECV                                             !0      
  134     1      > JMPZ                                                     !0, ->4
          2    >   QM_ASSIGN                                        ~1      '%E2%9C%85'
          3      > JMP                                                      ->5
          4    >   QM_ASSIGN                                        ~1      '%E2%9D%8C'
          5    > > RETURN                                                   ~1
  135     6*     > RETURN                                                   null

End of function test_results

End of class TestMagicMethods.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.87 ms | 1485 KiB | 15 Q