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?? var_dump(accessPrivatePropertyOf($t, 'translator2', 'translator3', 'x'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EjdNt
function name:  (null)
number of ops:  20
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                                               'var_dump'
         11        INIT_FCALL                                               'accessprivatepropertyof'
         12        SEND_VAR                                                 !0
         13        SEND_VAL                                                 'translator2'
         14        SEND_VAL                                                 'translator3'
         15        SEND_VAL                                                 'x'
         16        DO_FCALL                                      0  $8      
         17        SEND_VAR                                                 $8
         18        DO_ICALL                                                 
         19      > 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 = 64
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: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 64
Branch analysis from position: 30
Branch analysis from position: 64
filename:       /in/EjdNt
function name:  accessPrivatePropertyOf
number of ops:  82
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, ->64
   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
   68    58        INIT_FCALL_BY_NAME                                       'accessPrivatePropertyOf'
         59        SEND_VAR_EX                                              !3
         60        SEND_UNPACK                                              !1
         61        CHECK_UNDEF_ARGS                                         
         62        DO_FCALL                                      1  $37     
         63      > RETURN                                                   $37
   71    64    >   INIT_METHOD_CALL                                         !2, 'getProperty'
         65        SEND_VAR_EX                                              !3
         66        DO_FCALL                                      0  $38     
         67        ASSIGN                                                   !5, $38
   72    68        INIT_METHOD_CALL                                         !5, 'setAccessible'
         69        SEND_VAL_EX                                              <true>
         70        DO_FCALL                                      0          
   73    71        INIT_METHOD_CALL                                         !5, 'getValue'
         72        SEND_VAR_EX                                              !0
         73        DO_FCALL                                      0  $41     
         74        ASSIGN                                                   !3, $41
   75    75        INIT_FCALL_BY_NAME                                       'accessPrivatePropertyOf'
         76        SEND_VAR_EX                                              !3
         77        SEND_UNPACK                                              !1
         78        CHECK_UNDEF_ARGS                                         
         79        DO_FCALL                                      1  $43     
         80      > RETURN                                                   $43
   76    81*     > 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/EjdNt
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/EjdNt
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/EjdNt
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:
165.53 ms | 1415 KiB | 18 Q