3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace RefactoringGuru\Prototype\RealWorld; /** * Прототип. */ class Page { private $title; private $body; /** * @var Author */ private $author; private $comments = []; /** * @var \DateTime */ private $date; // +100 приватных полей. public function __construct(string $title, string $body, Author $author) { $this->title = $title; $this->body = $body; $this->author = $author; $this->author->addToPage($this); $this->date = new \DateTime(); } public function addComment(string $comment): void { $this->comments[] = $comment; } /** * Вы можете контролировать, какие данные вы хотите перенести в * клонированный объект. * * Например, при клонировании страницы: * - Она получает новый заголовок «Копия ...». * - Автор страницы остаётся прежним. Поэтому мы оставляем ссылку на * существующий объект, добавляя клонированную страницу в список страниц * автора. * - Мы не переносим комментарии со старой страницы. * - Мы также прикрепляем к странице новый объект даты. */ public function __clone() { $this->title = "Copy of " . $this->title; $this->author->addToPage($this); $this->comments = []; $this->date = new \DateTime(); } } class Author { private $name; /** * @var Page[] */ private $pages = []; public function __construct(string $name) { $this->name = $name; } public function addToPage(Page $page): void { $this->pages[] = $page; } } /** * Клиентский код. */ function clientCode() { $author = new Author("John Smith"); $page = new Page("Tip of the day", "Keep calm and carry on.", $author); // ... $page->addComment("Nice tip, thanks!"); // ... $draft = clone $page; echo "Dump of the clone. Note that the author is now referencing two objects.\n\n"; print_r($draft); } clientCode();
Output for git.master
Dump of the clone. Note that the author is now referencing two objects. RefactoringGuru\Prototype\RealWorld\Page Object ( [title:RefactoringGuru\Prototype\RealWorld\Page:private] => Copy of Tip of the day [body:RefactoringGuru\Prototype\RealWorld\Page:private] => Keep calm and carry on. [author:RefactoringGuru\Prototype\RealWorld\Page:private] => RefactoringGuru\Prototype\RealWorld\Author Object ( [name:RefactoringGuru\Prototype\RealWorld\Author:private] => John Smith [pages:RefactoringGuru\Prototype\RealWorld\Author:private] => Array ( [0] => RefactoringGuru\Prototype\RealWorld\Page Object ( [title:RefactoringGuru\Prototype\RealWorld\Page:private] => Tip of the day [body:RefactoringGuru\Prototype\RealWorld\Page:private] => Keep calm and carry on. [author:RefactoringGuru\Prototype\RealWorld\Page:private] => RefactoringGuru\Prototype\RealWorld\Author Object *RECURSION* [comments:RefactoringGuru\Prototype\RealWorld\Page:private] => Array ( [0] => Nice tip, thanks! ) [date:RefactoringGuru\Prototype\RealWorld\Page:private] => DateTime Object ( [date] => 2021-06-11 22:15:55.004087 [timezone_type] => 3 [timezone] => Europe/Amsterdam ) ) [1] => RefactoringGuru\Prototype\RealWorld\Page Object *RECURSION* ) ) [comments:RefactoringGuru\Prototype\RealWorld\Page:private] => Array ( ) [date:RefactoringGuru\Prototype\RealWorld\Page:private] => DateTime Object ( [date] => 2021-06-11 22:15:55.004096 [timezone_type] => 3 [timezone] => Europe/Amsterdam ) )
Output for git.master_jit
Dump of the clone. Note that the author is now referencing two objects. RefactoringGuru\Prototype\RealWorld\Page Object ( [title:RefactoringGuru\Prototype\RealWorld\Page:private] => Copy of Tip of the day [body:RefactoringGuru\Prototype\RealWorld\Page:private] => Keep calm and carry on. [author:RefactoringGuru\Prototype\RealWorld\Page:private] => RefactoringGuru\Prototype\RealWorld\Author Object ( [name:RefactoringGuru\Prototype\RealWorld\Author:private] => John Smith [pages:RefactoringGuru\Prototype\RealWorld\Author:private] => Array ( [0] => RefactoringGuru\Prototype\RealWorld\Page Object ( [title:RefactoringGuru\Prototype\RealWorld\Page:private] => Tip of the day [body:RefactoringGuru\Prototype\RealWorld\Page:private] => Keep calm and carry on. [author:RefactoringGuru\Prototype\RealWorld\Page:private] => RefactoringGuru\Prototype\RealWorld\Author Object *RECURSION* [comments:RefactoringGuru\Prototype\RealWorld\Page:private] => Array ( [0] => Nice tip, thanks! ) [date:RefactoringGuru\Prototype\RealWorld\Page:private] => DateTime Object ( [date] => 2021-06-11 22:15:55.004042 [timezone_type] => 3 [timezone] => Europe/Amsterdam ) ) [1] => RefactoringGuru\Prototype\RealWorld\Page Object *RECURSION* ) ) [comments:RefactoringGuru\Prototype\RealWorld\Page:private] => Array ( ) [date:RefactoringGuru\Prototype\RealWorld\Page:private] => DateTime Object ( [date] => 2021-06-11 22:15:55.004052 [timezone_type] => 3 [timezone] => Europe/Amsterdam ) )
Output for rfc.property-hooks
Dump of the clone. Note that the author is now referencing two objects. RefactoringGuru\Prototype\RealWorld\Page Object ( [title:RefactoringGuru\Prototype\RealWorld\Page:private] => Copy of Tip of the day [body:RefactoringGuru\Prototype\RealWorld\Page:private] => Keep calm and carry on. [author:RefactoringGuru\Prototype\RealWorld\Page:private] => RefactoringGuru\Prototype\RealWorld\Author Object ( [name:RefactoringGuru\Prototype\RealWorld\Author:private] => John Smith [pages:RefactoringGuru\Prototype\RealWorld\Author:private] => Array ( [0] => RefactoringGuru\Prototype\RealWorld\Page Object ( [title:RefactoringGuru\Prototype\RealWorld\Page:private] => Tip of the day [body:RefactoringGuru\Prototype\RealWorld\Page:private] => Keep calm and carry on. [author:RefactoringGuru\Prototype\RealWorld\Page:private] => RefactoringGuru\Prototype\RealWorld\Author Object *RECURSION* [comments:RefactoringGuru\Prototype\RealWorld\Page:private] => Array ( [0] => Nice tip, thanks! ) [date:RefactoringGuru\Prototype\RealWorld\Page:private] => DateTime Object ( [date] => 2021-06-11 22:15:55.003499 [timezone_type] => 3 [timezone] => Europe/Amsterdam ) ) [1] => RefactoringGuru\Prototype\RealWorld\Page Object *RECURSION* ) ) [comments:RefactoringGuru\Prototype\RealWorld\Page:private] => Array ( ) [date:RefactoringGuru\Prototype\RealWorld\Page:private] => DateTime Object ( [date] => 2021-06-11 22:15:55.003504 [timezone_type] => 3 [timezone] => Europe/Amsterdam ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
40.39 ms | 423 KiB | 5 Q