3v4l.org

run code in 300+ PHP versions simultaneously
<?php class MessageId { protected static $partSeperator = '_'; protected static $hashAlgorithm = 'crc32'; protected static $validNames = ['foo']; protected $name; protected $time; protected $hash; public function __construct($name) { $this->time = microtime(true); $this->name = trim($name); $this->hash = hash(static::$hashAlgorithm, rand(0, $this->time)); if (empty($this->name)) { throw new DomainException('Name must not be empty'); } if (!in_array($this->name, static::$validNames)) { throw new DomainException("Invalid name [$name]"); } } public static function fromString($idString) { $parts = explode(static::$partSeperator, $idString); if (count($parts) != 3) { throw new DomainException("Unexpected format [$idString]"); } return static::fromParts($parts[0], $parts[1], $parts[2]); } public static function fromParts($name, $time, $hash) { $id = new static($name); $id->time = floatval($time); $id->hash = strval($hash); return $id; } public function name() { return $this->name; } public function time() { return $this->time; } public function equals(self $other) { return ((string) $this == (string) $other); } public function __toString() { return implode(static::$partSeperator, (array) $this); } } $foo = new MessageId('foo'); var_dump((string) $foo);

preferences:
37.08 ms | 402 KiB | 5 Q