3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait PropertyValidator { private function validate(string $prop, mixed $value): mixed { $validator = (new ReflectionObject($this))->getProperty($prop)->getAttributes()[0]?->newInstance(); if (!$validator->validate($value)) { throw new \InvalidArgumentException(); } return $value; } } #[\Attribute] class Str { public function __construct(public readonly int $maxLength) {} public function validate(string $val): bool { return strlen($val) <= $this->maxLength; } } class User { use PropertyValidator; #[Str(5)] public string $name { set => $this->validate(__PROPERTY__, $value); } public function __construct( ) {} } $u = new User(); // This should be fine. $u->name = 'Crell'; // This throws. $u->name = 'Alfredo';

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.5.00.0100.00817.37

preferences:
146.82 ms | 1005 KiB | 7 Q