3v4l.org

run code in 300+ PHP versions simultaneously
<?php // returns true if property is safely accessible publicly by using $obj->$prop: function public_property_exists( $obj, $prop ){ // allow magic $obj->__isset( $prop ) to execute if exists if( isset( $obj->$prop )) return true; // no public/protected/private property exists with this name if( ! property_exists( $obj, $prop )) return false; // the property exists, but is it public? $rp = new ReflectionProperty( $obj, $prop ); return $rp->isPublic(); } class C { public $public = "I’m public!"; protected $protected = "I’m public!"; private $private = "I’m public!"; function __isset( $k ){ return substr( $k, 0, 5 ) === 'magic'; } function __get( $k ){ if( $k === 'magic_isset_but_null') return null; return "I’m {$k}!"; } } $o = new C(); foreach( array( 'public', 'protected', 'private', 'magic', 'magic_isset_but_null', 'missing' ) as $prop ){ if( public_property_exists( $o, $prop )) echo "\$o->{$prop} is a public property, its value is: ", var_export( $o->$prop, true ), "\n"; else echo "\$o->{$prop} is not a public property.\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 27
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 27
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 22
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
filename:       /in/QBTd1
function name:  (null)
number of ops:  29
compiled vars:  !0 = $o, !1 = $prop
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   NEW                                              $2      'C'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   31     3      > FE_RESET_R                                       $5      <array>, ->27
          4    > > FE_FETCH_R                                               $5, !1, ->27
   35     5    >   INIT_FCALL                                               'public_property_exists'
          6        SEND_VAR                                                 !0
          7        SEND_VAR                                                 !1
          8        DO_FCALL                                      0  $6      
          9      > JMPZ                                                     $6, ->22
   36    10    >   ROPE_INIT                                     3  ~8      '%24o-%3E'
         11        ROPE_ADD                                      1  ~8      ~8, !1
         12        ROPE_END                                      2  ~7      ~8, '+is+a+public+property%2C+its+value+is%3A+'
         13        ECHO                                                     ~7
   37    14        INIT_FCALL                                               'var_export'
         15        FETCH_OBJ_R                                      ~10     !0, !1
         16        SEND_VAL                                                 ~10
         17        SEND_VAL                                                 <true>
         18        DO_ICALL                                         $11     
         19        ECHO                                                     $11
         20        ECHO                                                     '%0A'
         21      > JMP                                                      ->26
   38    22    >   ROPE_INIT                                     3  ~13     '%24o-%3E'
         23        ROPE_ADD                                      1  ~13     ~13, !1
         24        ROPE_END                                      2  ~12     ~13, '+is+not+a+public+property.%0A'
         25        ECHO                                                     ~12
   31    26    > > JMP                                                      ->4
         27    >   FE_FREE                                                  $5
   39    28      > RETURN                                                   1

Function public_property_exists:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QBTd1
function name:  public_property_exists
number of ops:  21
compiled vars:  !0 = $obj, !1 = $prop, !2 = $rp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    6     2        ISSET_ISEMPTY_PROP_OBJ                                   !0, !1
          3      > JMPZ                                                     ~3, ->5
          4    > > RETURN                                                   <true>
    9     5    >   INIT_FCALL                                               'property_exists'
          6        SEND_VAR                                                 !0
          7        SEND_VAR                                                 !1
          8        DO_ICALL                                         $4      
          9        BOOL_NOT                                         ~5      $4
         10      > JMPZ                                                     ~5, ->12
         11    > > RETURN                                                   <false>
   12    12    >   NEW                                              $6      'ReflectionProperty'
         13        SEND_VAR_EX                                              !0
         14        SEND_VAR_EX                                              !1
         15        DO_FCALL                                      0          
         16        ASSIGN                                                   !2, $6
   13    17        INIT_METHOD_CALL                                         !2, 'isPublic'
         18        DO_FCALL                                      0  $9      
         19      > RETURN                                                   $9
   14    20*     > RETURN                                                   null

End of function public_property_exists

Class C:
Function __isset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QBTd1
function name:  __isset
number of ops:  9
compiled vars:  !0 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
   21     1        INIT_FCALL                                               'substr'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 0
          4        SEND_VAL                                                 5
          5        DO_ICALL                                         $1      
          6        IS_IDENTICAL                                     ~2      $1, 'magic'
          7      > RETURN                                                   ~2
   22     8*     > RETURN                                                   null

End of function __isset

Function __get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QBTd1
function name:  __get
number of ops:  9
compiled vars:  !0 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
   24     1        IS_IDENTICAL                                             !0, 'magic_isset_but_null'
          2      > JMPZ                                                     ~1, ->4
          3    > > RETURN                                                   null
   25     4    >   ROPE_INIT                                     3  ~3      'I%E2%80%99m+'
          5        ROPE_ADD                                      1  ~3      ~3, !0
          6        ROPE_END                                      2  ~2      ~3, '%21'
          7      > RETURN                                                   ~2
   26     8*     > RETURN                                                   null

End of function __get

End of class C.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.31 ms | 1411 KiB | 20 Q