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']() );
Output for 8.4.9 - 8.4.18
array(1) { ["/wp/v2/users"]=> &object(Resolvable)#3 (2) { ["callable":protected]=> object(Closure)#2 (3) { ["name"]=> string(22) "{closure:/in/v6Xio:54}" ["file"]=> string(9) "/in/v6Xio" ["line"]=> int(54) } ["resolved":protected]=> array(2) { [0]=> array(4) { ["method"]=> string(3) "GET" ["callback"]=> object(Closure)#4 (3) { ["name"]=> string(22) "{closure:/in/v6Xio:85}" ["file"]=> string(9) "/in/v6Xio" ["line"]=> int(85) } ["args"]=> array(1) { ["id"]=> array(0) { } } ["permission_callback"]=> string(13) "__return_true" } [1]=> array(3) { ["method"]=> string(4) "POST" ["callback"]=> object(Closure)#6 (3) { ["name"]=> string(35) "{closure:{closure:/in/v6Xio:54}:64}" ["file"]=> string(9) "/in/v6Xio" ["line"]=> int(64) } ["args"]=> array(1) { ["id"]=> array(0) { } } } } } } string(66) "Sorry, you are not allowed to access users without authentication."
Output for 8.3.5 - 8.3.18
array(1) { ["/wp/v2/users"]=> &object(Resolvable)#3 (2) { ["callable":protected]=> object(Closure)#2 (0) { } ["resolved":protected]=> array(2) { [0]=> array(4) { ["method"]=> string(3) "GET" ["callback"]=> object(Closure)#4 (0) { } ["args"]=> array(1) { ["id"]=> array(0) { } } ["permission_callback"]=> string(13) "__return_true" } [1]=> array(3) { ["method"]=> string(4) "POST" ["callback"]=> object(Closure)#6 (0) { } ["args"]=> array(1) { ["id"]=> array(0) { } } } } } } string(66) "Sorry, you are not allowed to access users without authentication."

preferences:
54.21 ms | 508 KiB | 4 Q