3v4l.org

run code in 300+ PHP versions simultaneously
<?php function sql(callable $expr): array { if ($context = (new \ReflectionFunction($expr))->getClosureThis()) { $placeholder = new class($context) { private object $context; private ReflectionObject $reflection; public function __construct(object $context) { $this->context = $context; $this->reflection = new \ReflectionObject($context); } public function __call($method, $args) { $method = $this->reflection->getMethod($method); return $method->getClosure($this->context)->call($this->context, ...$args); } public function __get($property) { $property = $this->reflection->getProperty($property); $property->setAccessible(true); return $property->getValue($this->context); } public function __isset($property): bool { return $this->reflection->hasProperty($property); } public function __set($property, $value): void { $property = $this->reflection->getProperty($property); $property->setAccessible(true); $property->setValue($this->context, $value); } public function __toString(): string { return '?'; } }; } else { $placeholder = new class { public function __toString(): string { return '?'; } }; } $expr = Closure::fromCallable($expr) ->bindTo($placeholder, $placeholder); $params = []; $generator = $expr(); while ($generator->valid()) { $params[] = $generator->current(); // Get the value from "yield" $generator->send('this'); // Insert placeholder } return [$generator->getReturn(), $params]; } class Foo { private int $id = 42; public function bar(): void { [$query, $params] = sql(fn() => "SELECT * FROM users WHERE id = ${yield $this->id} OR username = ${yield $this->username()}"); var_dump( $query, $params, ); } private function username(): string { return 'azjezz'; } } $f = new Foo(); $f->bar();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h00Uq
function name:  (null)
number of ops:  6
compiled vars:  !0 = $f
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   NEW                                              $1      'Foo'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   85     3        INIT_METHOD_CALL                                         !0, 'bar'
          4        DO_FCALL                                      0          
          5      > RETURN                                                   1

Function sql:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 31
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 31
Branch analysis from position: 41
Branch analysis from position: 31
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
filename:       /in/h00Uq
function name:  sql
number of ops:  49
compiled vars:  !0 = $expr, !1 = $context, !2 = $placeholder, !3 = $params, !4 = $generator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        NEW                                              $5      'ReflectionFunction'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        INIT_METHOD_CALL                                         $5, 'getClosureThis'
          5        DO_FCALL                                      0  $7      
          6        ASSIGN                                           ~8      !1, $7
          7      > JMPZ                                                     ~8, ->14
    6     8    >   DECLARE_ANON_CLASS                               <unknown> 
          9        NEW                                              $10     $9
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0          
         12        ASSIGN                                                   !2, $10
    5    13      > JMP                                                      ->18
   47    14    >   DECLARE_ANON_CLASS                               <unknown> 
         15        NEW                                              $14     $13
         16        DO_FCALL                                      0          
         17        ASSIGN                                                   !2, $14
   54    18    >   INIT_STATIC_METHOD_CALL                                  'Closure', 'fromCallable'
         19        SEND_VAR                                                 !0
         20        DO_FCALL                                      0  $17     
   55    21        INIT_METHOD_CALL                                         $17, 'bindTo'
         22        SEND_VAR_EX                                              !2
         23        SEND_VAR_EX                                              !2
         24        DO_FCALL                                      0  $18     
   54    25        ASSIGN                                                   !0, $18
   57    26        ASSIGN                                                   !3, <array>
   58    27        INIT_DYNAMIC_CALL                                        !0
         28        DO_FCALL                                      0  $21     
         29        ASSIGN                                                   !4, $21
   59    30      > JMP                                                      ->38
   60    31    >   INIT_METHOD_CALL                                         !4, 'current'
         32        DO_FCALL                                      0  $24     
         33        ASSIGN_DIM                                               !3
         34        OP_DATA                                                  $24
   61    35        INIT_METHOD_CALL                                         !4, 'send'
         36        SEND_VAL_EX                                              'this'
         37        DO_FCALL                                      0          
   59    38    >   INIT_METHOD_CALL                                         !4, 'valid'
         39        DO_FCALL                                      0  $26     
         40      > JMPNZ                                                    $26, ->31
   64    41    >   INIT_METHOD_CALL                                         !4, 'getReturn'
         42        DO_FCALL                                      0  $27     
         43        INIT_ARRAY                                       ~28     $27
         44        ADD_ARRAY_ELEMENT                                ~28     !3
         45        VERIFY_RETURN_TYPE                                       ~28
         46      > RETURN                                                   ~28
   65    47*       VERIFY_RETURN_TYPE                                       
         48*     > RETURN                                                   null

End of function sql

Class class@anonymous:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h00Uq
function name:  __construct
number of ops:  9
compiled vars:  !0 = $context
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
   11     1        ASSIGN_OBJ                                               'context'
          2        OP_DATA                                                  !0
   12     3        NEW                                              $3      'ReflectionObject'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0          
          6        ASSIGN_OBJ                                               'reflection'
          7        OP_DATA                                                  $3
   13     8      > 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/h00Uq
function name:  __call
number of ops:  21
compiled vars:  !0 = $method, !1 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   17     2        FETCH_OBJ_R                                      ~2      'reflection'
          3        INIT_METHOD_CALL                                         ~2, 'getMethod'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0  $3      
          6        ASSIGN                                                   !0, $3
   19     7        INIT_METHOD_CALL                                         !0, 'getClosure'
          8        CHECK_FUNC_ARG                                           
          9        FETCH_OBJ_FUNC_ARG                               $5      'context'
         10        SEND_FUNC_ARG                                            $5
         11        DO_FCALL                                      0  $6      
         12        INIT_METHOD_CALL                                         $6, 'call'
         13        CHECK_FUNC_ARG                                           
         14        FETCH_OBJ_FUNC_ARG                               $7      'context'
         15        SEND_FUNC_ARG                                            $7
         16        SEND_UNPACK                                              !1
         17        CHECK_UNDEF_ARGS                                         
         18        DO_FCALL                                      1  $8      
         19      > RETURN                                                   $8
   20    20*     > RETURN                                                   null

End of function __call

Function __get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h00Uq
function name:  __get
number of ops:  16
compiled vars:  !0 = $property
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
   24     1        FETCH_OBJ_R                                      ~1      'reflection'
          2        INIT_METHOD_CALL                                         ~1, 'getProperty'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $2      
          5        ASSIGN                                                   !0, $2
   25     6        INIT_METHOD_CALL                                         !0, 'setAccessible'
          7        SEND_VAL_EX                                              <true>
          8        DO_FCALL                                      0          
   27     9        INIT_METHOD_CALL                                         !0, 'getValue'
         10        CHECK_FUNC_ARG                                           
         11        FETCH_OBJ_FUNC_ARG                               $5      'context'
         12        SEND_FUNC_ARG                                            $5
         13        DO_FCALL                                      0  $6      
         14      > RETURN                                                   $6
   28    15*     > RETURN                                                   null

End of function __get

Function __isset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h00Uq
function name:  __isset
number of ops:  9
compiled vars:  !0 = $property
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   32     1        FETCH_OBJ_R                                      ~1      'reflection'
          2        INIT_METHOD_CALL                                         ~1, 'hasProperty'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $2      
          5        VERIFY_RETURN_TYPE                                       $2
          6      > RETURN                                                   $2
   33     7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of function __isset

Function __set:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h00Uq
function name:  __set
number of ops:  17
compiled vars:  !0 = $property, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   37     2        FETCH_OBJ_R                                      ~2      'reflection'
          3        INIT_METHOD_CALL                                         ~2, 'getProperty'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0  $3      
          6        ASSIGN                                                   !0, $3
   38     7        INIT_METHOD_CALL                                         !0, 'setAccessible'
          8        SEND_VAL_EX                                              <true>
          9        DO_FCALL                                      0          
   39    10        INIT_METHOD_CALL                                         !0, 'setValue'
         11        CHECK_FUNC_ARG                                           
         12        FETCH_OBJ_FUNC_ARG                               $6      'context'
         13        SEND_FUNC_ARG                                            $6
         14        SEND_VAR_EX                                              !1
         15        DO_FCALL                                      0          
   40    16      > RETURN                                                   null

End of function __set

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h00Uq
function name:  __toString
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E > > RETURN                                                   '%3F'
   44     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function __tostring

End of class class@anonymous.

Class class@anonymous:
Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h00Uq
function name:  __toString
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E > > RETURN                                                   '%3F'
   50     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function __tostring

End of class class@anonymous.

Class Foo:
Function bar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h00Uq
function name:  bar
number of ops:  14
compiled vars:  !0 = $query, !1 = $params
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   INIT_FCALL                                               'sql'
          1        DECLARE_LAMBDA_FUNCTION                          ~2      [0]
          2        SEND_VAL                                                 ~2
          3        DO_FCALL                                      0  $3      
          4        FETCH_LIST_R                                     $4      $3, 0
          5        ASSIGN                                                   !0, $4
          6        FETCH_LIST_R                                     $6      $3, 1
          7        ASSIGN                                                   !1, $6
          8        FREE                                                     $3
   73     9        INIT_FCALL                                               'var_dump'
   74    10        SEND_VAR                                                 !0
   75    11        SEND_VAR                                                 !1
   73    12        DO_ICALL                                                 
   77    13      > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 161) Position 1 = -2
filename:       /in/h00Uq
function name:  {closure}
number of ops:  16
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   GENERATOR_CREATE                                         
          1        ROPE_INIT                                     4  ~9      'SELECT+%2A+FROM+users+WHERE+id+%3D+'
          2        FETCH_THIS                                       $0      
          3        FETCH_OBJ_R                                      ~1      $0, 'id'
          4        YIELD                                            $2      ~1
          5        FETCH_R                      local               ~3      $2
          6        ROPE_ADD                                      1  ~9      ~9, ~3
          7        ROPE_ADD                                      2  ~9      ~9, '+OR+username+%3D+'
          8        FETCH_THIS                                       $4      
          9        INIT_METHOD_CALL                                         $4, 'username'
         10        DO_FCALL                                      0  $5      
         11        YIELD                                            $6      $5
         12        FETCH_R                      local               ~7      $6
         13        ROPE_END                                      3  ~8      ~9, ~7
         14      > GENERATOR_RETURN                                         
         15*     > GENERATOR_RETURN                                         

End of Dynamic Function 0

End of function bar

Function username:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h00Uq
function name:  username
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E > > RETURN                                                   'azjezz'
   81     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function username

End of class Foo.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.47 ms | 1019 KiB | 15 Q