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 class Answer extends Exception { public function getAnswer($object) { $class = new ReflectionClass(get_class($object)); $property = $class->getProperty("answer"); $property->setAccessible(true); return $property->getValue($object); } } $e = new Answer; // end editing here new Question($e);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XNcIC
function name:  (null)
number of ops:  8
compiled vars:  !0 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   NEW                                              $1      'Answer'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   37     3        NEW                                              $4      'Question'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0          
          6        FREE                                                     $4
          7      > 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/XNcIC
function name:  __construct
number of ops:  11
compiled vars:  !0 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
   12     1      > THROW                                         0          !0
          2*       JMP                                                      ->10
   13     3  E > > CATCH                                       last         'Exception'
   14     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
   16    10      > RETURN                                                   null

End of function __construct

End of class Question.

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

End of function getanswer

End of class Answer.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.06 ms | 1399 KiB | 13 Q