3v4l.org

run code in 500+ PHP versions simultaneously
<?php namespace Fake\BuiltIn\Functions; /** * Executes the given statements using fake built-in functions. * * @param callable $statements Statements to execute. * @return mixed Whatever $statements returns. * @throws \ReflectionException */ function fake(callable $statements) { $function = new \ReflectionFunction($statements); $start_line = $function->getStartLine(); $end_line = $function->getEndLine(); $function_source = implode('', array_slice(file($function->getFileName()), $start_line - 1, $end_line - $start_line + 1)); if (preg_match('/(?<={).*(?=})/s', $function_source, $matches)) { $function_body = $matches[0]; $namespace = __NAMESPACE__; return eval(" namespace $namespace; $function_body "); } throw new \RuntimeException('Failed to execute statements.'); } function strlen($string) { return 'fake result'; } namespace My\Name\Space; use function Fake\BuiltIn\Functions\fake; class MyClass { /** * @throws \ReflectionException */ public function testMethodWithFakeStrlen() { fake(function () { echo strlen('foo'), PHP_EOL; }); } public function testMethodWithoutFakeStrlen() { echo strlen('foo'), PHP_EOL; } } $myObject = new MyClass(); try { $myObject->testMethodWithFakeStrlen(); $myObject->testMethodWithoutFakeStrlen(); } catch (\ReflectionException $e) { // ... }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 8
Branch analysis from position: 8
2 jumps found. (Code = 107) Position 1 = 9, Position 2 = -2
Branch analysis from position: 9
filename:       /in/LriLW
function name:  (null)
number of ops:  10
compiled vars:  !0 = $myObject, !1 = $e
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   61     0  E >   NEW                                                  $2      'My%5CName%5CSpace%5CMyClass'
          1        DO_FCALL                                          0          
          2        ASSIGN                                                       !0, $2
   64     3        INIT_METHOD_CALL                                             !0, 'testMethodWithFakeStrlen'
          4        DO_FCALL                                          0          
   65     5        INIT_METHOD_CALL                                             !0, 'testMethodWithoutFakeStrlen'
          6        DO_FCALL                                          0          
          7      > JMP                                                          ->9
   66     8  E > > CATCH                                           last         'ReflectionException'
   68     9    > > RETURN                                                       1

Function fake%5Cbuiltin%5Cfunctions%5Cfake:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 208) Position 1 = 12, Position 2 = 30
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 61
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 61
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 61
Branch analysis from position: 51
Branch analysis from position: 61
filename:       /in/LriLW
function name:  Fake\BuiltIn\Functions\fake
number of ops:  66
compiled vars:  !0 = $statements, !1 = $function, !2 = $start_line, !3 = $end_line, !4 = $function_source, !5 = $matches, !6 = $function_body, !7 = $namespace
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   12     0  E >   RECV                                                 !0      
   14     1        NEW                                                  $8      'ReflectionFunction'
          2        SEND_VAR_EX                                                  !0
          3        DO_FCALL                                          0          
          4        ASSIGN                                                       !1, $8
   16     5        INIT_METHOD_CALL                                             !1, 'getStartLine'
          6        DO_FCALL                                          0  $11     
          7        ASSIGN                                                       !2, $11
   17     8        INIT_METHOD_CALL                                             !1, 'getEndLine'
          9        DO_FCALL                                          0  $13     
         10        ASSIGN                                                       !3, $13
   18    11      > JMP_FRAMELESS                                   s112         'fake%5Cbuiltin%5Cfunctions%5Cimplode', ->30
         12    >   INIT_NS_FCALL_BY_NAME                                        'Fake%5CBuiltIn%5CFunctions%5Cimplode'
         13        SEND_VAL_EX                                                  ''
   19    14        INIT_NS_FCALL_BY_NAME                                        'Fake%5CBuiltIn%5CFunctions%5Carray_slice'
         15        INIT_NS_FCALL_BY_NAME                                        'Fake%5CBuiltIn%5CFunctions%5Cfile'
         16        INIT_METHOD_CALL                                             !1, 'getFileName'
         17        DO_FCALL                                          0  $15     
         18        SEND_VAR_NO_REF_EX                                           $15
         19        DO_FCALL                                          0  $16     
         20        SEND_VAR_NO_REF_EX                                           $16
         21        SUB                                                  ~17     !2, 1
         22        SEND_VAL_EX                                                  ~17
         23        SUB                                                  ~18     !3, !2
         24        ADD                                                  ~19     ~18, 1
         25        SEND_VAL_EX                                                  ~19
         26        DO_FCALL                                          0  $20     
         27        SEND_VAR_NO_REF_EX                                           $20
   18    28        DO_FCALL                                          0  $21     
         29      > JMP                                                          ->44
   19    30    >   INIT_NS_FCALL_BY_NAME                                        'Fake%5CBuiltIn%5CFunctions%5Carray_slice'
         31        INIT_NS_FCALL_BY_NAME                                        'Fake%5CBuiltIn%5CFunctions%5Cfile'
         32        INIT_METHOD_CALL                                             !1, 'getFileName'
         33        DO_FCALL                                          0  $22     
         34        SEND_VAR_NO_REF_EX                                           $22
         35        DO_FCALL                                          0  $23     
         36        SEND_VAR_NO_REF_EX                                           $23
         37        SUB                                                  ~24     !2, 1
         38        SEND_VAL_EX                                                  ~24
         39        SUB                                                  ~25     !3, !2
         40        ADD                                                  ~26     ~25, 1
         41        SEND_VAL_EX                                                  ~26
         42        DO_FCALL                                          0  $27     
   18    43        FRAMELESS_ICALL_2                implode             $21     '', $27
         44    >   ASSIGN                                                       !4, $21
   21    45        INIT_NS_FCALL_BY_NAME                                        'Fake%5CBuiltIn%5CFunctions%5Cpreg_match'
         46        SEND_VAL_EX                                                  '%2F%28%3F%3C%3D%7B%29.%2A%28%3F%3D%7D%29%2Fs'
         47        SEND_VAR_EX                                                  !4
         48        SEND_VAR_EX                                                  !5
         49        DO_FCALL                                          0  $29     
         50      > JMPZ                                                         $29, ->61
   22    51    >   FETCH_DIM_R                                          ~30     !5, 0
         52        ASSIGN                                                       !6, ~30
   23    53        ASSIGN                                                       !7, 'Fake%5CBuiltIn%5CFunctions'
   25    54        ROPE_INIT                                         5  ~34     '%0A++++++namespace+'
   26    55        ROPE_ADD                                          1  ~34     ~34, !7
         56        ROPE_ADD                                          2  ~34     ~34, '%3B%0A++++++'
   27    57        ROPE_ADD                                          3  ~34     ~34, !6
         58        ROPE_END                                          4  ~33     ~34, '%0A++++'
         59        INCLUDE_OR_EVAL                                      $37     ~33, EVAL
         60      > RETURN                                                       $37
   31    61    >   NEW                                                  $38     'RuntimeException'
         62        SEND_VAL_EX                                                  'Failed+to+execute+statements.'
         63        DO_FCALL                                          0          
         64      > THROW                                             0          $38
   32    65*     > RETURN                                                       null

End of function fake%5Cbuiltin%5Cfunctions%5Cfake

Function fake%5Cbuiltin%5Cfunctions%5Cstrlen:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LriLW
function name:  Fake\BuiltIn\Functions\strlen
number of ops:  3
compiled vars:  !0 = $string
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   34     0  E >   RECV                                                 !0      
   36     1      > RETURN                                                       'fake+result'
   37     2*     > RETURN                                                       null

End of function fake%5Cbuiltin%5Cfunctions%5Cstrlen

Class My\Name\Space\MyClass:
Function testmethodwithfakestrlen:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LriLW
function name:  testMethodWithFakeStrlen
number of ops:  5
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   50     0  E >   INIT_FCALL                                                   'fake%5Cbuiltin%5Cfunctions%5Cfake'
          1        DECLARE_LAMBDA_FUNCTION                              ~0      [0]
   52     2        SEND_VAL                                                     ~0
   50     3        DO_FCALL                                          0          
   53     4      > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LriLW
function name:  {closure:My\Name\Space\MyClass::testMethodWithFakeStrlen():50}
number of ops:  7
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   51     0  E >   INIT_NS_FCALL_BY_NAME                                        'My%5CName%5CSpace%5Cstrlen'
          1        SEND_VAL_EX                                                  'foo'
          2        DO_FCALL                                          0  $0      
          3        ECHO                                                         $0
          4        FETCH_CONSTANT                                       ~1      'My%5CName%5CSpace%5CPHP_EOL'
          5        ECHO                                                         ~1
   52     6      > RETURN                                                       null

End of Dynamic Function 0

End of function testmethodwithfakestrlen

Function testmethodwithoutfakestrlen:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LriLW
function name:  testMethodWithoutFakeStrlen
number of ops:  7
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   57     0  E >   INIT_NS_FCALL_BY_NAME                                        'My%5CName%5CSpace%5Cstrlen'
          1        SEND_VAL_EX                                                  'foo'
          2        DO_FCALL                                          0  $0      
          3        ECHO                                                         $0
          4        FETCH_CONSTANT                                       ~1      'My%5CName%5CSpace%5CPHP_EOL'
          5        ECHO                                                         ~1
   58     6      > RETURN                                                       null

End of function testmethodwithoutfakestrlen

End of class My\Name\Space\MyClass.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
161.27 ms | 2123 KiB | 19 Q