3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class AbstractEvent { protected $id; private $hidden; } final class Event extends AbstractEvent implements JsonSerializable { private $value; public function __construct($id, self $value = null) { $this->id = 5; $this->value = 'abc'; } public function jsonSerialize() { return get_class_vars(); } /** * @param array $data * @return self */ public static function factory(array $data) { $params = []; $constructor = new ReflectionMethod(self::class, '__construct'); foreach ($constructor->getParameters() as $parameter) { $name = $parameter->getName(); $required = !$parameter->isOptional(); echo $parameter->getClass()->name; $params[] = self::getValue($data, $name, $required); } return new self(...$params); } } $e = Event::factory([]); echo json_encode($e);
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Return type of Event::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/b2tIh on line 20 Deprecated: Method ReflectionParameter::getClass() is deprecated in /in/b2tIh on line 37 Warning: Attempt to read property "name" on null in /in/b2tIh on line 37 Fatal error: Uncaught Error: Call to undefined method Event::getValue() in /in/b2tIh:38 Stack trace: #0 /in/b2tIh(46): Event::factory(Array) #1 {main} thrown in /in/b2tIh on line 38
Process exited with code 255.
Output for 8.0.0 - 8.0.30
Deprecated: Method ReflectionParameter::getClass() is deprecated in /in/b2tIh on line 37 Warning: Attempt to read property "name" on null in /in/b2tIh on line 37 Fatal error: Uncaught Error: Call to undefined method Event::getValue() in /in/b2tIh:38 Stack trace: #0 /in/b2tIh(46): Event::factory(Array) #1 {main} thrown in /in/b2tIh on line 38
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.33
Notice: Trying to get property 'name' of non-object in /in/b2tIh on line 37 Fatal error: Uncaught Error: Call to undefined method Event::getValue() in /in/b2tIh:38 Stack trace: #0 /in/b2tIh(46): Event::factory(Array) #1 {main} thrown in /in/b2tIh on line 38
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Uncaught Error: Call to undefined method Event::getValue() in /in/b2tIh:38 Stack trace: #0 /in/b2tIh(46): Event::factory(Array) #1 {main} thrown in /in/b2tIh on line 38
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20
Notice: Trying to get property of non-object in /in/b2tIh on line 37 Fatal error: Uncaught Error: Call to undefined method Event::getValue() in /in/b2tIh:38 Stack trace: #0 /in/b2tIh(46): Event::factory(Array) #1 {main} thrown in /in/b2tIh on line 38
Process exited with code 255.
Output for 5.6.0 - 5.6.24
Notice: Trying to get property of non-object in /in/b2tIh on line 37 Fatal error: Call to undefined method Event::getValue() in /in/b2tIh on line 38
Process exited with code 255.
Output for 5.5.0 - 5.5.38
Parse error: syntax error, unexpected '.' in /in/b2tIh on line 41
Process exited with code 255.
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /in/b2tIh on line 33
Process exited with code 255.

preferences:
240.6 ms | 401 KiB | 267 Q