3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Logger { public function log($data) { // Actually log your data echo 'LOGGING DATA: ' . $data . PHP_EOL; } } class Hassan { public function multiplyByTwo($i) { // Doing business logic $i = (int)$i * 2; return $i; } } class HassanLoggerDecorator { protected $hassan; protected $logger; public function __construct(Hassan $hassan, Logger $logger) { $this->hassan = $hassan; $this->logger = $logger; } public function __call($method, $args) { $this->logger->log(sprintf("Calling method: %s with args: %s", $method, print_r($args, true))); $result = call_user_func_array( array($this->hassan, $method), $args ); $this->logger->log(sprintf("Got result: %s", $result)); return $result; } } // Now you can use your Hassan object without logging... $hassan = new Hassan; echo "3 multipled by two is: " . $hassan->multiplyByTwo(3) . PHP_EOL . PHP_EOL; // Now let's log the output of Hassan, without having it as a dependency!! (Never FORCE logging in anything you do, it's ALWAYS optional) $hassanWithLoggingFunctionality = new HassanLoggerDecorator($hassan, new Logger); echo $hassanWithLoggingFunctionality->multiplyByTwo(3);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MXmJ3
function name:  (null)
number of ops:  22
compiled vars:  !0 = $hassan, !1 = $hassanWithLoggingFunctionality
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   NEW                                              $2      'Hassan'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   53     3        INIT_METHOD_CALL                                         !0, 'multiplyByTwo'
          4        SEND_VAL_EX                                              3
          5        DO_FCALL                                      0  $5      
          6        CONCAT                                           ~6      '3+multipled+by+two+is%3A+', $5
          7        CONCAT                                           ~7      ~6, '%0A'
          8        CONCAT                                           ~8      ~7, '%0A'
          9        ECHO                                                     ~8
   56    10        NEW                                              $9      'HassanLoggerDecorator'
         11        SEND_VAR_EX                                              !0
         12        NEW                                              $10     'Logger'
         13        DO_FCALL                                      0          
         14        SEND_VAR_NO_REF_EX                                       $10
         15        DO_FCALL                                      0          
         16        ASSIGN                                                   !1, $9
   58    17        INIT_METHOD_CALL                                         !1, 'multiplyByTwo'
         18        SEND_VAL_EX                                              3
         19        DO_FCALL                                      0  $14     
         20        ECHO                                                     $14
         21      > RETURN                                                   1

Class Logger:
Function log:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MXmJ3
function name:  log
number of ops:  5
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
    8     1        CONCAT                                           ~1      'LOGGING+DATA%3A+', !0
          2        CONCAT                                           ~2      ~1, '%0A'
          3        ECHO                                                     ~2
    9     4      > RETURN                                                   null

End of function log

End of class Logger.

Class Hassan:
Function multiplybytwo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MXmJ3
function name:  multiplyByTwo
number of ops:  6
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   17     1        CAST                                          4  ~1      !0
          2        MUL                                              ~2      ~1, 2
          3        ASSIGN                                                   !0, ~2
   19     4      > RETURN                                                   !0
   20     5*     > RETURN                                                   null

End of function multiplybytwo

End of class Hassan.

Class HassanLoggerDecorator:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MXmJ3
function name:  __construct
number of ops:  7
compiled vars:  !0 = $hassan, !1 = $logger
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   31     2        ASSIGN_OBJ                                               'hassan'
          3        OP_DATA                                                  !0
   32     4        ASSIGN_OBJ                                               'logger'
          5        OP_DATA                                                  !1
   33     6      > RETURN                                                   null

End of function __construct

Function __call:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MXmJ3
function name:  __call
number of ops:  33
compiled vars:  !0 = $method, !1 = $args, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   37     2        FETCH_OBJ_R                                      ~3      'logger'
          3        INIT_METHOD_CALL                                         ~3, 'log'
          4        INIT_FCALL                                               'sprintf'
          5        SEND_VAL                                                 'Calling+method%3A+%25s+with+args%3A+%25s'
          6        SEND_VAR                                                 !0
          7        INIT_FCALL                                               'print_r'
          8        SEND_VAR                                                 !1
          9        SEND_VAL                                                 <true>
         10        DO_ICALL                                         $4      
         11        SEND_VAR                                                 $4
         12        DO_ICALL                                         $5      
         13        SEND_VAR_NO_REF_EX                                       $5
         14        DO_FCALL                                      0          
   40    15        FETCH_OBJ_R                                      ~7      'hassan'
         16        INIT_ARRAY                                       ~8      ~7
         17        ADD_ARRAY_ELEMENT                                ~8      !0
         18        INIT_USER_CALL                                0          'call_user_func_array', ~8
   41    19        SEND_ARRAY                                               !1
         20        CHECK_UNDEF_ARGS                                         
         21        DO_FCALL                                      0  $9      
   39    22        ASSIGN                                                   !2, $9
   44    23        FETCH_OBJ_R                                      ~11     'logger'
         24        INIT_METHOD_CALL                                         ~11, 'log'
         25        INIT_FCALL                                               'sprintf'
         26        SEND_VAL                                                 'Got+result%3A+%25s'
         27        SEND_VAR                                                 !2
         28        DO_ICALL                                         $12     
         29        SEND_VAR_NO_REF_EX                                       $12
         30        DO_FCALL                                      0          
   46    31      > RETURN                                                   !2
   47    32*     > RETURN                                                   null

End of function __call

End of class HassanLoggerDecorator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.98 ms | 1404 KiB | 17 Q