3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Translator1 { private $translator2; public function __construct(Translator2 $t2) { $this->translator2 = $t2; } } class Translator2 { private $translator3; public function __construct(Translator3 $t3) { $this->translator3 = $t3; } } class Translator3 extends ParentTranslator { // none found, look for 'x' in parent class } class ParentTranslator { private $x = 'LOL'; public function __construct() { $this->x = 'LOL'; } } function accessPrivatePropertyOf($object, ...$properties) { if (empty($properties)) { return $object; } $reflector = new \ReflectionClass($object); $property = array_shift($properties); echo 'Looking in: ' . (is_object($object) ? get_class($object) : gettype($object)) . ' for property: ' . $property . PHP_EOL; if (!$reflector->hasProperty($property)) { /** Try parent class **/ $parent = $reflector->getParentClass(); echo 'BUT... does not exist, looking in parent: ' . $object->getName() . ' instead...' . PHP_EOL; if (!$parent->hasProperty($property)) { throw new \Exception; } $prop = $parent->getProperty($property); $prop->setAccessible(true); $property = $prop->getValue($object); echo 'We found property with name: ' . $prop->getName() . ' and value: ' . $prop->getValue($object) . PHP_EOL; return accessPrivatePropertyOf($property, ...$properties); } $prop = $reflector->getProperty($property); $prop->setAccessible(true); $property = $prop->getValue($object); return accessPrivatePropertyOf($property, ...$properties); } $t = new Translator1(new Translator2(new Translator3)); // Shows no value for x WHYY?Y?? accessPrivatePropertyOf($t, 'translator2', 'translator3', 'x');
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WkeGP
function name:  (null)
number of ops:  17
compiled vars:  !0 = $t
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   DECLARE_CLASS                                            'translator3', 'parenttranslator'
   78     1        NEW                                              $1      'Translator1'
          2        NEW                                              $2      'Translator2'
          3        NEW                                              $3      'Translator3'
          4        DO_FCALL                                      0          
          5        SEND_VAR_NO_REF_EX                                       $3
          6        DO_FCALL                                      0          
          7        SEND_VAR_NO_REF_EX                                       $2
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !0, $1
   81    10        INIT_FCALL                                               'accessprivatepropertyof'
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 'translator2'
         13        SEND_VAL                                                 'translator3'
         14        SEND_VAL                                                 'x'
         15        DO_FCALL                                      0          
         16      > RETURN                                                   1

Function accessprivatepropertyof:
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 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 74
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 47
Branch analysis from position: 44
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 74
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 74
Branch analysis from position: 30
Branch analysis from position: 74
filename:       /in/WkeGP
function name:  accessPrivatePropertyOf
number of ops:  92
compiled vars:  !0 = $object, !1 = $properties, !2 = $reflector, !3 = $property, !4 = $parent, !5 = $prop
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
          1        RECV_VARIADIC                                    !1      
   39     2        ISSET_ISEMPTY_CV                                         !1
          3      > JMPZ                                                     ~6, ->5
   41     4    > > RETURN                                                   !0
   44     5    >   NEW                                              $7      'ReflectionClass'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !2, $7
   46     9        INIT_FCALL                                               'array_shift'
         10        SEND_REF                                                 !1
         11        DO_ICALL                                         $10     
         12        ASSIGN                                                   !3, $10
   48    13        TYPE_CHECK                                  256          !0
         14      > JMPZ                                                     ~12, ->18
         15    >   GET_CLASS                                        ~13     !0
         16        QM_ASSIGN                                        ~14     ~13
         17      > JMP                                                      ->20
         18    >   GET_TYPE                                         ~15     !0
         19        QM_ASSIGN                                        ~14     ~15
         20    >   CONCAT                                           ~16     'Looking+in%3A+', ~14
         21        CONCAT                                           ~17     ~16, '+for+property%3A+'
         22        CONCAT                                           ~18     ~17, !3
         23        CONCAT                                           ~19     ~18, '%0A'
         24        ECHO                                                     ~19
   50    25        INIT_METHOD_CALL                                         !2, 'hasProperty'
         26        SEND_VAR_EX                                              !3
         27        DO_FCALL                                      0  $20     
         28        BOOL_NOT                                         ~21     $20
         29      > JMPZ                                                     ~21, ->74
   53    30    >   INIT_METHOD_CALL                                         !2, 'getParentClass'
         31        DO_FCALL                                      0  $22     
         32        ASSIGN                                                   !4, $22
   55    33        INIT_METHOD_CALL                                         !0, 'getName'
         34        DO_FCALL                                      0  $24     
         35        CONCAT                                           ~25     'BUT...+does+not+exist%2C+looking+in+parent%3A+', $24
         36        CONCAT                                           ~26     ~25, '+instead...'
         37        CONCAT                                           ~27     ~26, '%0A'
         38        ECHO                                                     ~27
   57    39        INIT_METHOD_CALL                                         !4, 'hasProperty'
         40        SEND_VAR_EX                                              !3
         41        DO_FCALL                                      0  $28     
         42        BOOL_NOT                                         ~29     $28
         43      > JMPZ                                                     ~29, ->47
   59    44    >   NEW                                              $30     'Exception'
         45        DO_FCALL                                      0          
         46      > THROW                                         0          $30
   62    47    >   INIT_METHOD_CALL                                         !4, 'getProperty'
         48        SEND_VAR_EX                                              !3
         49        DO_FCALL                                      0  $32     
         50        ASSIGN                                                   !5, $32
   63    51        INIT_METHOD_CALL                                         !5, 'setAccessible'
         52        SEND_VAL_EX                                              <true>
         53        DO_FCALL                                      0          
   64    54        INIT_METHOD_CALL                                         !5, 'getValue'
         55        SEND_VAR_EX                                              !0
         56        DO_FCALL                                      0  $35     
         57        ASSIGN                                                   !3, $35
   66    58        INIT_METHOD_CALL                                         !5, 'getName'
         59        DO_FCALL                                      0  $37     
         60        CONCAT                                           ~38     'We+found+property+with+name%3A+', $37
         61        CONCAT                                           ~39     ~38, '+and+value%3A+'
         62        INIT_METHOD_CALL                                         !5, 'getValue'
         63        SEND_VAR_EX                                              !0
         64        DO_FCALL                                      0  $40     
         65        CONCAT                                           ~41     ~39, $40
         66        CONCAT                                           ~42     ~41, '%0A'
         67        ECHO                                                     ~42
   68    68        INIT_FCALL_BY_NAME                                       'accessPrivatePropertyOf'
         69        SEND_VAR_EX                                              !3
         70        SEND_UNPACK                                              !1
         71        CHECK_UNDEF_ARGS                                         
         72        DO_FCALL                                      1  $43     
         73      > RETURN                                                   $43
   71    74    >   INIT_METHOD_CALL                                         !2, 'getProperty'
         75        SEND_VAR_EX                                              !3
         76        DO_FCALL                                      0  $44     
         77        ASSIGN                                                   !5, $44
   72    78        INIT_METHOD_CALL                                         !5, 'setAccessible'
         79        SEND_VAL_EX                                              <true>
         80        DO_FCALL                                      0          
   73    81        INIT_METHOD_CALL                                         !5, 'getValue'
         82        SEND_VAR_EX                                              !0
         83        DO_FCALL                                      0  $47     
         84        ASSIGN                                                   !3, $47
   75    85        INIT_FCALL_BY_NAME                                       'accessPrivatePropertyOf'
         86        SEND_VAR_EX                                              !3
         87        SEND_UNPACK                                              !1
         88        CHECK_UNDEF_ARGS                                         
         89        DO_FCALL                                      1  $49     
         90      > RETURN                                                   $49
   76    91*     > RETURN                                                   null

End of function accessprivatepropertyof

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

End of function __construct

End of class Translator1.

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

End of function __construct

End of class Translator2.

Class Translator3: [no user functions]
Class ParentTranslator:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WkeGP
function name:  __construct
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   ASSIGN_OBJ                                               'x'
          1        OP_DATA                                                  'LOL'
   34     2      > RETURN                                                   null

End of function __construct

End of class ParentTranslator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.21 ms | 1411 KiB | 16 Q