3v4l.org

run code in 500+ PHP versions simultaneously
<?php abstract class Operation { protected static ?\ReflectionMethod $constructReflectionMethod = null; protected static ?array $abstractConstructParams = null; public function __construct( protected ?string $name = null, protected ?string $foo = null, ) {} } class Get extends Operation { private static ?array $constructParams = null; public function __construct( ?string $name = null, ?string $bar = null, protected string $method = 'GET', ) { if (null === self::$constructParams) { $rc = new \ReflectionClass($this); foreach ($rc->getMethod('__construct')->getParameters() as $v) { self::$constructParams[] = $v->getName(); } if (null === parent::$constructReflectionMethod) { parent::$constructReflectionMethod = $rc->getParentClass()->getMethod('__construct'); foreach (parent::$constructReflectionMethod->getParameters() as $v) { self::$abstractConstructParams[] = $v->getName(); } } } foreach (func_get_args() as $i => $v) { $a[self::$constructParams[$i]] = $v; } foreach (self::$abstractConstructParams as $parameter) { $args[$parameter] = $a[$parameter] ?? null; } parent::$constructReflectionMethod->invokeArgs($this, $args); } } var_dump(new Get(name: 'test', method: 'GET'));

preferences:
23.93 ms | 474 KiB | 4 Q