3v4l.org

run code in 500+ PHP versions simultaneously
<?php class Resolvable implements ArrayAccess, IteratorAggregate, Countable { protected $callable; protected $resolved = null; public function __construct( callable $closure ) { $this->callable = $closure; } public function __invoke( ...$args ) { if ( ! $this->resolved ) { $this->resolved = call_user_func_array( $this->callable, $args ); // Any normalisation can be done here. } return $this->resolved; } public function offsetExists( mixed $k ) : bool { $this->__invoke(); return isset( $this->resolved[ $k ] ); } public function &offsetGet( mixed $k ) : mixed { $this->__invoke(); return $this->resolved[ $k ]; } public function offsetSet( mixed $k, mixed $v ) : void { $this->__invoke(); $this->resolved[ $k ] = $v; } public function offsetUnset( mixed $k ) : void { $this->__invoke(); unset( $this->resolved[ $k ] ); } public function getIterator(): Traversable { $this->__invoke(); return new ArrayIterator( $this->resolved ); } public function count() : int { $this->__invoke(); return count( $this->resolved ); } } // $foo = function () { return 1; }; $endpoints = [ '/wp/v2/users' => fn () => [ [ 'method' => 'GET', 'callback' => fn () => true, 'args' => [ 'id' => [], ], ], [ 'method' => 'POST', 'callback' => fn () => true, 'args' => [ 'id' => [], ], ], ], ]; foreach ( $endpoints as $k => &$ep ) { if ( is_callable( $ep ) ) { $ep = new Resolvable( $ep ); } } // Old-style plugin, eg Really Simple SSL: if ( isset( $endpoints['/wp/v2/users'] ) ) { // Save the original endpoint $original_endpoint = $endpoints['/wp/v2/users']; // Override the GET callback $endpoints['/wp/v2/users'][0]['callback'] = function() { return 'Sorry, you are not allowed to access users without authentication.'; }; // Preserve the original args and permission callback $endpoints['/wp/v2/users'][0]['args'] = $original_endpoint[0]['args']; $endpoints['/wp/v2/users'][0]['permission_callback'] = '__return_true'; } var_dump( $endpoints ); var_dump( $endpoints['/wp/v2/users'][0]['callback']() );
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 7, Position 2 = 18
Branch analysis from position: 7
2 jumps found. (Code = 126) Position 1 = 8, Position 2 = 18
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 17
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 38
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
Branch analysis from position: 18
filename:       /in/v6Xio
function name:  (null)
number of ops:  50
compiled vars:  !0 = $foo, !1 = $endpoints, !2 = $ep, !3 = $k, !4 = $original_endpoint
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                                'resolvable'
   52     1        DECLARE_LAMBDA_FUNCTION                              ~5      [0]
          2        ASSIGN                                                       !0, ~5
   54     3        DECLARE_LAMBDA_FUNCTION                              ~7      [1]
   69     4        INIT_ARRAY                                           ~8      ~7, '%2Fwp%2Fv2%2Fusers'
   53     5        ASSIGN                                                       !1, ~8
   72     6      > FE_RESET_RW                                          $10     !1, ->18
          7    > > FE_FETCH_RW                                          ~11     $10, !2, ->18
          8    >   ASSIGN                                                       !3, ~11
   73     9        INIT_FCALL                                                   'is_callable'
         10        SEND_VAR                                                     !2
         11        DO_ICALL                                             $13     
         12      > JMPZ                                                         $13, ->17
   74    13    >   NEW                                                  $14     'Resolvable'
         14        SEND_VAR_EX                                                  !2
         15        DO_FCALL                                          0          
         16        ASSIGN                                                       !2, $14
   72    17    > > JMP                                                          ->7
         18    >   FE_FREE                                                      $10
   80    19        ISSET_ISEMPTY_DIM_OBJ                             0          !1, '%2Fwp%2Fv2%2Fusers'
         20      > JMPZ                                                         ~17, ->38
   82    21    >   FETCH_DIM_R                                          ~18     !1, '%2Fwp%2Fv2%2Fusers'
         22        ASSIGN                                                       !4, ~18
   85    23        DECLARE_LAMBDA_FUNCTION                              ~23     [2]
         24        FETCH_DIM_W                                          $20     !1, '%2Fwp%2Fv2%2Fusers'
         25        FETCH_DIM_W                                          $21     $20, 0
         26        ASSIGN_DIM                                                   $21, 'callback'
   87    27        OP_DATA                                                      ~23
   90    28        FETCH_DIM_R                                          ~27     !4, 0
         29        FETCH_DIM_R                                          ~28     ~27, 'args'
         30        FETCH_DIM_W                                          $24     !1, '%2Fwp%2Fv2%2Fusers'
         31        FETCH_DIM_W                                          $25     $24, 0
         32        ASSIGN_DIM                                                   $25, 'args'
         33        OP_DATA                                                      ~28
   91    34        FETCH_DIM_W                                          $29     !1, '%2Fwp%2Fv2%2Fusers'
         35        FETCH_DIM_W                                          $30     $29, 0
         36        ASSIGN_DIM                                                   $30, 'permission_callback'
         37        OP_DATA                                                      '__return_true'
   94    38    >   INIT_FCALL                                                   'var_dump'
         39        SEND_VAR                                                     !1
         40        DO_ICALL                                                     
   95    41        INIT_FCALL                                                   'var_dump'
         42        FETCH_DIM_R                                          ~33     !1, '%2Fwp%2Fv2%2Fusers'
         43        FETCH_DIM_R                                          ~34     ~33, 0
         44        FETCH_DIM_R                                          ~35     ~34, 'callback'
         45        INIT_DYNAMIC_CALL                                            ~35
         46        DO_FCALL                                          0  $36     
         47        SEND_VAR                                                     $36
         48        DO_ICALL                                                     
         49      > RETURN                                                       1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v6Xio
function name:  {closure:/in/v6Xio:52}
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   52     0  E > > RETURN                                                       1
          1*     > RETURN                                                       null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v6Xio
function name:  {closure:/in/v6Xio:54}
number of ops:  12
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   56     0  E >   INIT_ARRAY                                           ~0      'GET', 'method'
   57     1        DECLARE_LAMBDA_FUNCTION                              ~1      [0]
          2        ADD_ARRAY_ELEMENT                                    ~0      ~1, 'callback'
   56     3        ADD_ARRAY_ELEMENT                                    ~0      <array>, 'args'
          4        INIT_ARRAY                                           ~2      ~0
   63     5        INIT_ARRAY                                           ~3      'POST', 'method'
   64     6        DECLARE_LAMBDA_FUNCTION                              ~4      [1]
          7        ADD_ARRAY_ELEMENT                                    ~3      ~4, 'callback'
   56     8        ADD_ARRAY_ELEMENT                                    ~3      <array>, 'args'
          9        ADD_ARRAY_ELEMENT                                    ~2      ~3
         10      > RETURN                                                       ~2
   69    11*     > 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/v6Xio
function name:  {closure:{closure:/in/v6Xio:54}:57}
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   57     0  E > > RETURN                                                       <true>
          1*     > RETURN                                                       null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v6Xio
function name:  {closure:{closure:/in/v6Xio:54}:64}
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   64     0  E > > RETURN                                                       <true>
          1*     > RETURN                                                       null

End of Dynamic Function 1

End of Dynamic Function 1

Dynamic Function 2
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v6Xio
function name:  {closure:/in/v6Xio:85}
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   86     0  E > > RETURN                                                       'Sorry%2C+you+are+not+allowed+to+access+users+without+authentication.'
   87     1*     > RETURN                                                       null

End of Dynamic Function 2

Class Resolvable:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v6Xio
function name:  __construct
number of ops:  4
compiled vars:  !0 = $closure
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    7     0  E >   RECV                                                 !0      
    8     1        ASSIGN_OBJ                                                   'callable'
          2        OP_DATA                                                      !0
    9     3      > RETURN                                                       null

End of function __construct

Function __invoke:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/v6Xio
function name:  __invoke
number of ops:  14
compiled vars:  !0 = $args
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   11     0  E >   RECV_VARIADIC                                        !0      
   12     1        FETCH_OBJ_R                                          ~1      'resolved'
          2        BOOL_NOT                                             ~2      ~1
          3      > JMPZ                                                         ~2, ->11
   13     4    >   FETCH_OBJ_R                                          ~4      'callable'
          5        INIT_USER_CALL                                    0          'call_user_func_array', ~4
          6        SEND_ARRAY                                                   !0
          7        CHECK_UNDEF_ARGS                                             
          8        DO_FCALL                                          1  $5      
          9        ASSIGN_OBJ                                                   'resolved'
         10        OP_DATA                                                      $5
   17    11    >   FETCH_OBJ_R                                          ~6      'resolved'
         12      > RETURN                                                       ~6
   18    13*     > RETURN                                                       null

End of function __invoke

Function offsetexists:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v6Xio
function name:  offsetExists
number of ops:  9
compiled vars:  !0 = $k
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   20     0  E >   RECV                                                 !0      
   21     1        INIT_METHOD_CALL                                             '__invoke'
          2        DO_FCALL                                          0          
   22     3        FETCH_OBJ_IS                                         ~2      'resolved'
          4        ISSET_ISEMPTY_DIM_OBJ                             0  ~3      ~2, !0
          5        VERIFY_RETURN_TYPE                                           ~3
          6      > RETURN                                                       ~3
   23     7*       VERIFY_RETURN_TYPE                                           
          8*     > RETURN                                                       null

End of function offsetexists

Function offsetget:
Finding entry points
Branch analysis from position: 0
Return found
filename:       /in/v6Xio
function name:  offsetGet
number of ops:  8
compiled vars:  !0 = $k
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   25     0  E >   RECV                                                 !0      
   26     1        INIT_METHOD_CALL                                             '__invoke'
          2        DO_FCALL                                          0          
   27     3        FETCH_OBJ_W                                          $2      'resolved'
          4        FETCH_DIM_W                                          $3      $2, !0
          5      > RETURN_BY_REF                                                $3
   28     6*       VERIFY_RETURN_TYPE                                           
          7*     > RETURN_BY_REF                                                null

End of function offsetget

Function offsetset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v6Xio
function name:  offsetSet
number of ops:  8
compiled vars:  !0 = $k, !1 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   30     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   31     2        INIT_METHOD_CALL                                             '__invoke'
          3        DO_FCALL                                          0          
   32     4        FETCH_OBJ_W                                          $3      'resolved'
          5        ASSIGN_DIM                                                   $3, !0
          6        OP_DATA                                                      !1
   33     7      > RETURN                                                       null

End of function offsetset

Function offsetunset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v6Xio
function name:  offsetUnset
number of ops:  6
compiled vars:  !0 = $k
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   35     0  E >   RECV                                                 !0      
   36     1        INIT_METHOD_CALL                                             '__invoke'
          2        DO_FCALL                                          0          
   37     3        FETCH_OBJ_UNSET                                      $2      'resolved'
          4        UNSET_DIM                                                    $2, !0
   38     5      > RETURN                                                       null

End of function offsetunset

Function getiterator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v6Xio
function name:  getIterator
number of ops:  11
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   41     0  E >   INIT_METHOD_CALL                                             '__invoke'
          1        DO_FCALL                                          0          
   42     2        NEW                                                  $1      'ArrayIterator'
          3        CHECK_FUNC_ARG                                               
          4        FETCH_OBJ_FUNC_ARG                                   $2      'resolved'
          5        SEND_FUNC_ARG                                                $2
          6        DO_FCALL                                          0          
          7        VERIFY_RETURN_TYPE                                           $1
          8      > RETURN                                                       $1
   43     9*       VERIFY_RETURN_TYPE                                           
         10*     > RETURN                                                       null

End of function getiterator

Function count:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v6Xio
function name:  count
number of ops:  8
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   46     0  E >   INIT_METHOD_CALL                                             '__invoke'
          1        DO_FCALL                                          0          
   47     2        FETCH_OBJ_R                                          ~1      'resolved'
          3        COUNT                                                ~2      ~1
          4        VERIFY_RETURN_TYPE                                           ~2
          5      > RETURN                                                       ~2
   48     6*       VERIFY_RETURN_TYPE                                           
          7*     > RETURN                                                       null

End of function count

End of class Resolvable.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
153.97 ms | 1136 KiB | 15 Q