3v4l.org

run code in 500+ PHP versions simultaneously
<?php $email = Email::create('TEST@fluentbe.com'); var_dump($email); readonly class Email { final protected function __construct( private string $value, ) { } public static function create(string $value): static { $data = static::recreate($value); $data->guard(); return $data; } public static function recreate(string $value): static { return new static($value); } public function getValue(): string { return $this->value; } public function equals(self $other): bool { return $this->getValue() === $other->getValue(); } protected function guard(): void { if (preg_match("/^(?!\.)(?!.*\.\.)([a-zA-Z0-9_'+\-.]*)[a-zA-Z0-9_'+\-]@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/", $this->getValue()) !== 1) { throw new \InvalidArgumentException($this->getValue()); } } }
Output for 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.6
object(Email)#1 (1) { ["value":"Email":private]=> string(17) "TEST@fluentbe.com" }

preferences:
41.84 ms | 707 KiB | 4 Q