3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class FormModel { private array $attributes; private array $attributesLabels; private array $attributesErrors = []; public function __construct() { $this->attributes = $this->attributes(); $this->attributesLabels = $this->attributeLabels(); } public function getAttributeValue(string $attribute) { return $this->readProperty($attribute); } public function attributeHint(string $attribute): string { $hints = $this->attributeHints(); return $hints[$attribute] ?? ''; } public function attributeHints(): array { return []; } public function attributeLabel(string $attribute): string { return $this->attributesLabels[$attribute] ?? $this->generateAttributeLabel($attribute); } public function formName(): string { return ''; } public function hasAttribute(string $attribute): bool { return \array_key_exists($attribute, $this->attributes); } public function error(string $attribute): array { return $this->attributesErrors[$attribute] ?? []; } public function errors(): array { return $this->attributesErrors ?? []; } public function errorSummary(bool $showAllErrors): array { $lines = []; $errors = $showAllErrors ? $this->errors() : [$this->firstErrors()]; foreach ($errors as $error) { $lines = \array_merge($lines, $error); } return $lines; } public function firstError(string $attribute): string { return isset($this->attributesErrors[$attribute]) ? reset($this->attributesErrors[$attribute]) : ''; } public function firstErrors(): array { if (empty($this->attributesErrors)) { return []; } $errors = []; foreach ($this->attributesErrors as $name => $es) { if (!empty($es)) { $errors[$name] = \reset($es); } } return $errors; } public function hasErrors(?string $attribute = null): bool { return $attribute === null ? !empty($this->attributesErrors) : isset($this->attributesErrors[$attribute]); } public function load(array $data): bool { $result = false; $scope = $this->formName(); if ($scope === '' && !empty($data)) { $this->setAttributes($data); $result = true; } elseif (isset($data[$scope])) { $this->setAttributes($data[$scope]); $result = true; } return $result; } public function setAttributes(array $values): void { foreach ($values as $name => $value) { if (isset($this->attributes[$name])) { switch ($this->attributes[$name]) { case 'bool': $this->writeProperty($name, (bool) $value); break; case 'float': $this->writeProperty($name, (float) $value); break; case 'int': $this->writeProperty($name, (int) $value); break; default: $this->writeProperty($name, $value); break; } } } } protected function addError(string $attribute, string $error): void { $this->attributesErrors[$attribute][] = $error; } private function addErrors(array $items): void { foreach ($items as $attribute => $errors) { foreach ((array)$errors as $error) { $this->attributesErrors[$attribute][] = $error; } } } private function attributes(): array { $class = new \ReflectionClass($this); foreach ($class->getProperties() as $property) { if (!$property->isStatic()) { $type = (new \ReflectionProperty($property->class, $property->name))->getType(); if ($type !== null) { $this->attributes[$property->getName()] = $type->getName(); } else { throw new \InvalidArgumentException( "You must specify the type hint for \"$property->class\" class." ); } } } return $this->attributes; } private function clearErrors(?string $attribute = null): void { if ($attribute === null) { $this->attributesErrors = []; } else { unset($this->attributesErrors[$attribute]); } } private function readProperty(string $attribute) { $class = get_class($this); if (!property_exists($class, $attribute)) { throw new InvalidArgumentException("Undefined property: \"$class::$attribute\"."); } $getter = fn ($class, $attribute) => $class->$attribute; $getter = \Closure::bind($getter, null, $this); return $getter($this, $attribute); } private function writeProperty(string $attribute, $value): void { $setter = fn ($class, $attribute, $value) => $class->$attribute = $value; $setter = \Closure::bind($setter, null, $this); $setter($this, $attribute, $value); } } class LoginForm extends FormModel { private ?string $login = null; private ?string $password = null; private bool $rememberMe = false; public function getLogin(): ?string { return $this->login; } public function getPassword(): ?string { return $this->password; } public function getRememberMe(): bool { return $this->rememberMe; } public function login(string $value): void { $this->login = $value; } public function password(string $value): void { $this->password = $value; } public function rememberMe(bool $value): void { $this->rememberMe = $value; } public function attributeHints(): array { return [ 'login' => 'Write your id or email.', 'password' => 'Write your password.' ]; } public function attributeLabels(): array { return [ 'login' => 'Login:', 'password' => 'Password:', 'rememberMe' => 'remember Me:' ]; } public function formName(): string { return 'LoginForm'; } public function addError(string $attribute, string $error): void { parent::addError($attribute, $error); } } $form = new LoginForm(); $form->login('yiiliveext'); $form->addError('login', 'Login incorrect'); var_dump($form->errors());

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.4.120.0130.00822.37
8.4.110.0120.00822.49
8.4.100.0100.00917.79
8.4.90.0130.00918.67
8.4.80.0100.01018.01
8.4.70.0100.00717.83
8.4.60.0110.00518.82
8.4.50.0100.00618.84
8.4.40.0100.01017.68
8.4.30.0060.00318.66
8.4.20.0180.00422.15
8.4.10.0000.00822.39
8.3.250.0090.01019.31
8.3.240.0130.00716.50
8.3.230.0090.00716.68
8.3.220.0120.00717.14
8.3.210.0120.00918.36
8.3.200.0060.00316.82
8.3.190.0040.00517.22
8.3.180.0080.00116.81
8.3.170.0100.00218.94
8.3.160.0090.00917.16
8.3.150.0040.00817.22
8.3.140.0000.00816.96
8.3.130.0000.00916.82
8.3.120.0090.00021.01
8.3.110.0030.00620.94
8.3.100.0150.00024.06
8.3.90.0030.00526.77
8.3.80.0100.00019.36
8.3.70.0160.00318.56
8.3.60.0120.00316.88
8.3.50.0060.01123.63
8.3.40.0180.00419.41
8.3.30.0150.00018.92
8.3.20.0080.00024.18
8.3.10.0000.00924.66
8.3.00.0040.00426.16
8.2.290.0120.00720.48
8.2.280.0080.00818.20
8.2.270.0060.01218.96
8.2.260.0060.00318.57
8.2.250.0140.00718.39
8.2.240.0030.00618.75
8.2.230.0080.00022.58
8.2.220.0000.00937.54
8.2.210.0050.00326.77
8.2.200.0040.00418.88
8.2.190.0140.00716.88
8.2.180.0110.01125.92
8.2.170.0110.00422.96
8.2.160.0100.01022.96
8.2.150.0050.00325.66
8.2.140.0060.00324.66
8.2.130.0000.00826.16
8.2.120.0050.00326.35
8.2.110.0070.00317.63
8.2.100.0090.00317.78
8.2.90.0000.00817.63
8.2.80.0040.00418.99
8.2.70.0060.00317.63
8.2.60.0080.00017.75
8.2.50.0030.00618.05
8.2.40.0000.00920.01
8.2.30.0040.00418.36
8.2.20.0080.00018.30
8.2.10.0030.00519.49
8.2.00.0050.00319.48
8.1.330.0070.01122.16
8.1.320.0080.01116.13
8.1.310.0060.00316.59
8.1.300.0090.00016.15
8.1.290.0040.00830.84
8.1.280.0040.01125.92
8.1.270.0080.00023.99
8.1.260.0000.00828.09
8.1.250.0030.00528.09
8.1.240.0060.00323.84
8.1.230.0070.00421.09
8.1.220.0040.00417.74
8.1.210.0030.00518.77
8.1.200.0000.00917.54
8.1.190.0090.00017.50
8.1.180.0030.00618.10
8.1.170.0080.00019.14
8.1.160.0050.00318.92
8.1.150.0050.00219.01
8.1.140.0040.00419.02
8.1.130.0000.00720.17
8.1.120.0040.00417.60
8.1.110.0070.00017.43
8.1.100.0040.00417.47
8.1.90.0040.00417.64
8.1.80.0040.00417.49
8.1.70.0050.00217.46
8.1.60.0040.00417.71
8.1.50.0040.00417.66
8.1.40.0040.00417.69
8.1.30.0040.00417.77
8.1.20.0030.00617.77
8.1.10.0090.00017.72
8.1.00.0000.00817.51
8.0.300.0000.00818.77
8.0.290.0080.00016.75
8.0.280.0040.00418.63
8.0.270.0000.00718.18
8.0.260.0000.00718.57
8.0.250.0000.00716.98
8.0.240.0050.00217.11
8.0.230.0000.00817.11
8.0.220.0070.00017.01
8.0.210.0050.00217.00
8.0.200.0050.00317.06
8.0.190.0040.00417.04
8.0.180.0040.00417.02
8.0.170.0030.00617.00
8.0.160.0000.00817.02
8.0.150.0030.00516.95
8.0.140.0000.00716.99
8.0.130.0060.00013.42
8.0.120.0040.00416.96
8.0.110.0000.00816.97
8.0.100.0040.00416.91
8.0.90.0000.00916.98
8.0.80.0070.00817.00
8.0.70.0000.00817.08
8.0.60.0030.00516.92
8.0.50.0000.00716.91
8.0.30.0140.00417.13
8.0.20.0120.00717.15
8.0.10.0050.00317.16
8.0.00.0090.01216.88
7.4.330.0020.00215.55
7.4.320.0030.00316.59
7.4.300.0020.00516.62
7.4.290.0040.00416.70
7.4.280.0040.00416.77
7.4.270.0000.00716.73
7.4.260.0000.00513.46
7.4.250.0040.00416.58
7.4.240.0060.00216.69
7.4.230.0070.00016.71
7.4.220.0040.00316.59
7.4.210.0030.01116.73
7.4.200.0040.00316.51
7.4.130.0050.01216.55
7.4.120.0030.01416.67
7.4.110.0100.00716.63
7.4.100.0080.01216.69
7.4.90.0080.00816.64
7.4.80.0100.00719.39
7.4.70.0110.01116.60
7.4.60.0100.00716.63
7.4.50.0070.01516.48
7.4.40.0100.00716.43
7.4.30.0080.00816.61
7.4.20.0120.00916.45
7.4.10.0140.00616.52
7.4.00.0070.01116.47
7.3.330.0030.00616.23
7.3.320.0000.00613.02
7.3.310.0030.00316.11
7.3.300.0040.00416.21
7.3.290.0060.01016.15
7.3.280.0060.01316.19
7.3.260.0080.01016.21
7.3.230.0130.00616.17
7.3.210.0030.01916.20
7.3.200.0140.00416.15
7.3.190.0100.00616.39
7.3.180.0080.00916.13
7.3.170.0070.01016.12
7.3.160.0110.00516.04
7.3.150.0060.01016.22
7.3.140.0030.01616.20
7.3.130.0000.01715.99
7.3.120.0130.00316.25
7.3.110.0110.00716.09
7.3.100.0000.01516.11
7.3.90.0080.00815.98
7.3.80.0080.00816.05
7.3.70.0070.01016.10
7.3.60.0080.00816.06
7.3.50.0090.00616.18
7.3.40.0060.00916.16
7.3.30.0070.01016.19
7.3.20.0180.00016.02
7.3.10.0070.01016.07
7.3.00.0110.00416.05
7.2.330.0290.01516.44
7.2.320.0000.02116.46
7.2.310.0120.00616.40
7.2.300.0100.00816.25
7.2.290.0090.00616.42
7.2.280.0040.01216.02
7.2.270.0170.00316.00
7.2.260.0090.00916.29
7.2.250.0100.00716.29
7.2.240.0120.00616.09
7.2.230.0070.01016.17
7.2.220.0070.00716.38
7.2.210.0130.00316.29
7.2.200.0060.00916.35
7.2.190.0100.00616.07
7.2.180.0070.01016.28
7.2.170.0030.01316.45
7.2.160.0060.01216.36
7.2.150.0410.01016.47
7.2.140.0150.00916.08
7.2.130.0060.01116.22
7.2.120.0030.01416.21
7.2.110.0070.01016.25
7.2.100.0030.01416.32
7.2.90.0120.00616.32
7.2.80.0120.00416.16
7.2.70.0070.01116.30
7.2.60.0070.01316.35
7.2.50.0130.00416.42
7.2.40.0110.00616.32
7.2.30.0150.00016.18
7.2.20.0100.00716.31
7.2.10.0040.01316.09
7.2.00.0030.01216.16

preferences:
29.66 ms | 403 KiB | 5 Q