3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* Challenge 3: Use reflection to get access to Question::$answer from $e->getAnswer */ class Question { private $answer = 42; public function __construct($e) { try { throw $e; } catch (Exception $e) { echo $e->getAnswer($this) . PHP_EOL; } } } // start editing here $question = new Question(new Object); class E extends Exception{ public function getAnswer($question) { $reflectionClass = new ReflectionClass('Question'); $property = $reflectionClass->getProperty('answer'); $property->setAccessible(true); return $property->getValue($question); } } $e = new E; // end editing here new Question($e);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RJqn4
function name:  (null)
number of ops:  14
compiled vars:  !0 = $question, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   NEW                                              $2      'Question'
          1        NEW                                              $3      'Object'
          2        DO_FCALL                                      0          
          3        SEND_VAR_NO_REF_EX                                       $3
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $2
   34     6        NEW                                              $7      'E'
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !1, $7
   38     9        NEW                                              $10     'Question'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0          
         12        FREE                                                     $10
         13      > RETURN                                                   1

Class Question:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 108) Position 1 = -2
Found catch point at position: 3
Branch analysis from position: 3
2 jumps found. (Code = 107) Position 1 = 4, Position 2 = -2
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RJqn4
function name:  __construct
number of ops:  11
compiled vars:  !0 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
   14     1      > THROW                                         0          !0
          2*       JMP                                                      ->10
   15     3  E > > CATCH                                       last         'Exception'
   16     4    >   INIT_METHOD_CALL                                         !0, 'getAnswer'
          5        FETCH_THIS                                       $1      
          6        SEND_VAR_EX                                              $1
          7        DO_FCALL                                      0  $2      
          8        CONCAT                                           ~3      $2, '%0A'
          9        ECHO                                                     ~3
   18    10      > RETURN                                                   null

End of function __construct

End of class Question.

Class E:
Function getanswer:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RJqn4
function name:  getAnswer
number of ops:  17
compiled vars:  !0 = $question, !1 = $reflectionClass, !2 = $property
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   27     1        NEW                                              $3      'ReflectionClass'
          2        SEND_VAL_EX                                              'Question'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $3
   28     5        INIT_METHOD_CALL                                         !1, 'getProperty'
          6        SEND_VAL_EX                                              'answer'
          7        DO_FCALL                                      0  $6      
          8        ASSIGN                                                   !2, $6
   29     9        INIT_METHOD_CALL                                         !2, 'setAccessible'
         10        SEND_VAL_EX                                              <true>
         11        DO_FCALL                                      0          
   30    12        INIT_METHOD_CALL                                         !2, 'getValue'
         13        SEND_VAR_EX                                              !0
         14        DO_FCALL                                      0  $9      
         15      > RETURN                                                   $9
   31    16*     > RETURN                                                   null

End of function getanswer

End of class E.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.88 ms | 1390 KiB | 13 Q