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();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AnO8X
function name:  (null)
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  102     0  E >   INIT_NS_FCALL_BY_NAME                                    'RefactoringGuru%5CPrototype%5CRealWorld%5CclientCode'
          1        DO_FCALL                                      0          
          2      > RETURN                                                   1

Function refactoringguru%5Cprototype%5Crealworld%5Cclientcode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AnO8X
function name:  RefactoringGuru\Prototype\RealWorld\clientCode
number of ops:  20
compiled vars:  !0 = $author, !1 = $page, !2 = $draft
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   NEW                                              $3      'RefactoringGuru%5CPrototype%5CRealWorld%5CAuthor'
          1        SEND_VAL_EX                                              'John+Smith'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $3
   89     4        NEW                                              $6      'RefactoringGuru%5CPrototype%5CRealWorld%5CPage'
          5        SEND_VAL_EX                                              'Tip+of+the+day'
          6        SEND_VAL_EX                                              'Keep+calm+and+carry+on.'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !1, $6
   93    10        INIT_METHOD_CALL                                         !1, 'addComment'
         11        SEND_VAL_EX                                              'Nice+tip%2C+thanks%21'
         12        DO_FCALL                                      0          
   97    13        CLONE                                            ~10     !1
         14        ASSIGN                                                   !2, ~10
   98    15        ECHO                                                     'Dump+of+the+clone.+Note+that+the+author+is+now+referencing+two+objects.%0A%0A'
   99    16        INIT_NS_FCALL_BY_NAME                                    'RefactoringGuru%5CPrototype%5CRealWorld%5Cprint_r'
         17        SEND_VAR_EX                                              !2
         18        DO_FCALL                                      0          
  100    19      > RETURN                                                   null

End of function refactoringguru%5Cprototype%5Crealworld%5Cclientcode

Class RefactoringGuru\Prototype\RealWorld\Page:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AnO8X
function name:  __construct
number of ops:  19
compiled vars:  !0 = $title, !1 = $body, !2 = $author
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   30     3        ASSIGN_OBJ                                               'title'
          4        OP_DATA                                                  !0
   31     5        ASSIGN_OBJ                                               'body'
          6        OP_DATA                                                  !1
   32     7        ASSIGN_OBJ                                               'author'
          8        OP_DATA                                                  !2
   33     9        FETCH_OBJ_R                                      ~6      'author'
         10        INIT_METHOD_CALL                                         ~6, 'addToPage'
         11        FETCH_THIS                                       $7      
         12        SEND_VAR_EX                                              $7
         13        DO_FCALL                                      0          
   34    14        NEW                                              $10     'DateTime'
         15        DO_FCALL                                      0          
         16        ASSIGN_OBJ                                               'date'
         17        OP_DATA                                                  $10
   35    18      > RETURN                                                   null

End of function __construct

Function addcomment:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AnO8X
function name:  addComment
number of ops:  5
compiled vars:  !0 = $comment
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
   39     1        FETCH_OBJ_W                                      $1      'comments'
          2        ASSIGN_DIM                                               $1
          3        OP_DATA                                                  !0
   40     4      > RETURN                                                   null

End of function addcomment

Function __clone:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AnO8X
function name:  __clone
number of ops:  16
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   FETCH_OBJ_R                                      ~1      'title'
          1        CONCAT                                           ~2      'Copy+of+', ~1
          2        ASSIGN_OBJ                                               'title'
          3        OP_DATA                                                  ~2
   57     4        FETCH_OBJ_R                                      ~3      'author'
          5        INIT_METHOD_CALL                                         ~3, 'addToPage'
          6        FETCH_THIS                                       $4      
          7        SEND_VAR_EX                                              $4
          8        DO_FCALL                                      0          
   58     9        ASSIGN_OBJ                                               'comments'
         10        OP_DATA                                                  <array>
   59    11        NEW                                              $8      'DateTime'
         12        DO_FCALL                                      0          
         13        ASSIGN_OBJ                                               'date'
         14        OP_DATA                                                  $8
   60    15      > RETURN                                                   null

End of function __clone

End of class RefactoringGuru\Prototype\RealWorld\Page.

Class RefactoringGuru\Prototype\RealWorld\Author:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AnO8X
function name:  __construct
number of ops:  4
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   RECV                                             !0      
   74     1        ASSIGN_OBJ                                               'name'
          2        OP_DATA                                                  !0
   75     3      > RETURN                                                   null

End of function __construct

Function addtopage:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AnO8X
function name:  addToPage
number of ops:  5
compiled vars:  !0 = $page
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   RECV                                             !0      
   79     1        FETCH_OBJ_W                                      $1      'pages'
          2        ASSIGN_DIM                                               $1
          3        OP_DATA                                                  !0
   80     4      > RETURN                                                   null

End of function addtopage

End of class RefactoringGuru\Prototype\RealWorld\Author.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
180.37 ms | 1407 KiB | 16 Q