3v4l.org

run code in 500+ PHP versions simultaneously
<?php function ghost_func_num_args(string $class, string $method, array $vars = []) { return count(ghost_func_get_args($class, $method, $vars)); } function ghost_func_get_args(string $class, string $method, array $vars = []) { $argc = 0; $argv = []; $reflectionMethod = new ReflectionMethod($class, $method); foreach ($reflectionMethod->getParameters() as $parameter) { $name = $parameter->getName(); $values = $vars[$name] ?? null; if (! $parameter->isVariadic()) { $argv[$parameter->getPosition()] = $values; ++$argc; continue; } foreach ($values as $value) { $argv[$argc++] = $value; } } return $argv; } function ghost_func_get_named_args(string $class, string $method, array $vars = []) { $argv_named = []; $reflectionMethod = new ReflectionMethod($class, $method); foreach ($reflectionMethod->getParameters() as $parameter) { $name = $parameter->getName(); $values = $vars[$name] ?? null; if (! $parameter->isVariadic()) { $argv_named[$name] = $values; continue; } foreach ($values as $key => $value) { $argv_named[$key] = $value; } } return $argv_named; } class User { public function register($email, $password, $active = null, $default = 0, ...$attributes) { $__mockery__vars = get_defined_vars(); $argc = ghost_func_num_args(__CLASS__, __FUNCTION__, $__mockery__vars); $argv = ghost_func_get_args(__CLASS__, __FUNCTION__, $__mockery__vars); $argv_named = ghost_func_get_named_args(__CLASS__, __FUNCTION__, $__mockery__vars); print_r([ '$argc' => $argc, '$argv' => $argv, '$argv_named' => $argv_named, ]); return [$email, $password, $attributes]; } } $foo = new User(); $foo->register('admin@test.com', 'pass1', ...[ 'id' => 'uuid-1', 'phone' => '012-345-6789', 'role' => 'admin', ]); echo PHP_EOL . '--------' . PHP_EOL; $foo->register(...[ 'id' => 'uuid-2', 'email' => 'test@test.com', 'phone' => '987-654-3210', 'name' => 'first last2', 'role' => 'tester', 'password' => 'pass2' ]);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.33, 8.3.0 - 8.3.33, 8.4.1 - 8.4.25, 8.5.0 - 8.5.10
Array ( [$argc] => 7 [$argv] => Array ( [0] => admin@test.com [1] => pass1 [2] => [3] => 0 [4] => uuid-1 [5] => 012-345-6789 [6] => admin ) [$argv_named] => Array ( [email] => admin@test.com [password] => pass1 [active] => [default] => 0 [id] => uuid-1 [phone] => 012-345-6789 [role] => admin ) ) -------- Array ( [$argc] => 8 [$argv] => Array ( [0] => test@test.com [1] => pass2 [2] => [3] => 0 [4] => uuid-2 [5] => 987-654-3210 [6] => first last2 [7] => tester ) [$argv_named] => Array ( [email] => test@test.com [password] => pass2 [active] => [default] => 0 [id] => uuid-2 [phone] => 987-654-3210 [name] => first last2 [role] => tester ) )
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Fatal error: Uncaught Error: Cannot unpack array with string keys in /in/UvZt8:74 Stack trace: #0 {main} thrown in /in/UvZt8 on line 74
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Parse error: syntax error, unexpected '?' in /in/UvZt8 on line 14
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/UvZt8 on line 4
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/UvZt8 on line 4
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/UvZt8 on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting ')' in /in/UvZt8 on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/UvZt8 on line 4
Process exited with code 255.

preferences:
66.49 ms | 2990 KiB | 4 Q