3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Assert { private const UUID_LENGTH = 32; public static function validUuid(string $value) : string { if (strlen($value) !== static::UUID_LENGTH || false === ctype_xdigit($value)) { throw new \InvalidArgumentException('Wrong uuid format.'); } return __CLASS__; } public static function notEmptyString(string $value) : string { if ('' === $value) { throw new InvalidArgumentException('Value must not be empty.'); } return __CLASS__; } public static function notEmptyArray(array $value) : string { if (0 >= count($value)) { throw new InvalidArgumentException('Array must not be empty.'); } return __CLASS__; } } class Id { private string $value; public function __construct(?string $uuid = null) { $uuid = $uuid ?? $this->generateuuidV4(); Assert::validUuid($uuid); $this->value = $uuid; } public function __toString() : string { return $this->value; } public function isEqual(Id $id) : bool { return $this->value === (string)$id; } private function generateUuidV4() : string { return bin2hex(random_bytes(16)); } } interface Entity { public function getId() : Id; } class Comment implements Entity { private Id $id; private Id $userId; private string $text; public function __construct(Id $id, Id $userId, string $text) { Assert::notEmptyString($text); $this->id = $id; $this->userId = $userId; $this->text = $text; } public function getId() : Id { return $this->id; } public function getText() : string { return $this->text; } public function changeText(string $text) : void { $this->text = $text; } } class ModeratedComment extends Comment { private Comment $comment; public function __construct(Comment $comment) { $this->comment = $this->moderate($comment); } public function getId() : Id { return $this->comment->getId(); } public function getText() : string { return $this->comment->getText(); } public function changeText(string $text) : void { $this->comment->changeText($text); $this->comment = $this->moderate($this->comment); } private function moderate(Comment $comment) : Comment { $comment->changeText('Моча - сила'); return $comment; } } class Feed implements Entity { private Id $id; private Id $ownerId; private array $publishers; private array $comments; public function __construct( Id $id, Id $ownerId, array $publishers, array $comments = [] ) { Assert::notEmptyArray($publishers); $this->id = $id; $this->ownerId = $ownerId; $this->publishers = $publishers; $this->comments = $comments; } public function getId() : Id { return $this->id; } public function addComment(ModeratedComment $moderatedComment) { $this->comments[(string)$moderatedComment->getId()] = $moderatedComment; } } $feedId = new Id('3a90426a01e414d00f281ae92436f11d'); $feedOwnerId = new Id('da41d5f6b9ef685ddc3fc58f8f2b33f8'); $firstPublisherId = new Id('da41d5f6b9ef685ddc3fc58f8f2b33f8'); $secondPublisherId = new Id('76319d3a35d3ec306dc299d446ab46e6'); $publishers = [ (string)$firstPublisherId => $firstPublisherId, (string)$secondPublisherId => $secondPublisherId, ]; $feed = new Feed($feedId, $feedOwnerId, $publishers); $userId = new Id('da41d5f6b9ef685ddc3fc58f8f2b33f8'); $moderatedComment = new ModeratedComment(new Comment(new Id, $userId, 'Test comment')); $feed->addComment($moderatedComment); var_dump($feed);
Output for 7.4.1
object(Feed)#5 (4) { ["id":"Feed":private]=> object(Id)#1 (1) { ["value":"Id":private]=> string(32) "3a90426a01e414d00f281ae92436f11d" } ["ownerId":"Feed":private]=> object(Id)#2 (1) { ["value":"Id":private]=> string(32) "da41d5f6b9ef685ddc3fc58f8f2b33f8" } ["publishers":"Feed":private]=> array(2) { ["da41d5f6b9ef685ddc3fc58f8f2b33f8"]=> object(Id)#3 (1) { ["value":"Id":private]=> string(32) "da41d5f6b9ef685ddc3fc58f8f2b33f8" } ["76319d3a35d3ec306dc299d446ab46e6"]=> object(Id)#4 (1) { ["value":"Id":private]=> string(32) "76319d3a35d3ec306dc299d446ab46e6" } } ["comments":"Feed":private]=> array(1) { ["e8c84ec95664fbf98f8b8397982229c0"]=> object(ModeratedComment)#7 (1) { ["comment":"ModeratedComment":private]=> object(Comment)#8 (3) { ["id":"Comment":private]=> object(Id)#9 (1) { ["value":"Id":private]=> string(32) "e8c84ec95664fbf98f8b8397982229c0" } ["userId":"Comment":private]=> object(Id)#6 (1) { ["value":"Id":private]=> string(32) "da41d5f6b9ef685ddc3fc58f8f2b33f8" } ["text":"Comment":private]=> string(19) "Моча - сила" } ["id":"Comment":private]=> uninitialized(Id) ["userId":"Comment":private]=> uninitialized(Id) ["text":"Comment":private]=> uninitialized(string) } } }
Output for 7.4.0
object(Feed)#5 (4) { ["id":"Feed":private]=> object(Id)#1 (1) { ["value":"Id":private]=> string(32) "3a90426a01e414d00f281ae92436f11d" } ["ownerId":"Feed":private]=> object(Id)#2 (1) { ["value":"Id":private]=> string(32) "da41d5f6b9ef685ddc3fc58f8f2b33f8" } ["publishers":"Feed":private]=> array(2) { ["da41d5f6b9ef685ddc3fc58f8f2b33f8"]=> object(Id)#3 (1) { ["value":"Id":private]=> string(32) "da41d5f6b9ef685ddc3fc58f8f2b33f8" } ["76319d3a35d3ec306dc299d446ab46e6"]=> object(Id)#4 (1) { ["value":"Id":private]=> string(32) "76319d3a35d3ec306dc299d446ab46e6" } } ["comments":"Feed":private]=> array(1) { ["5011d964b32a6e148b0c19a58107440c"]=> object(ModeratedComment)#7 (1) { ["comment":"ModeratedComment":private]=> object(Comment)#8 (3) { ["id":"Comment":private]=> object(Id)#9 (1) { ["value":"Id":private]=> string(32) "5011d964b32a6e148b0c19a58107440c" } ["userId":"Comment":private]=> object(Id)#6 (1) { ["value":"Id":private]=> string(32) "da41d5f6b9ef685ddc3fc58f8f2b33f8" } ["text":"Comment":private]=> string(19) "Моча - сила" } ["id":"Comment":private]=> uninitialized(Id) ["userId":"Comment":private]=> uninitialized(Id) ["text":"Comment":private]=> uninitialized(string) } } }
Output for 7.2.0 - 7.2.26, 7.3.0 - 7.3.13
Parse error: syntax error, unexpected 'string' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/c81Oi on line 37
Process exited with code 255.

preferences:
71.82 ms | 402 KiB | 48 Q