3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace RefactoringGuru\Command\Conceptual; /** * Интерфейс Команды объявляет метод для выполнения команд. */ interface Command { public function execute(): void; } /** * Некоторые команды способны выполнять простые операции самостоятельно. */ class SimpleCommand implements Command { private $payload; public function __construct(string $payload) { $this->payload = $payload; } public function execute(): void { echo "SimpleCommand: See, I can do simple things like printing (" . $this->payload . ")\n"; } } /** * Но есть и команды, которые делегируют более сложные операции другим объектам, * называемым «получателями». */ class ComplexCommand implements Command { /** * @var Receiver */ private $receiver; /** * Данные о контексте, необходимые для запуска методов получателя. */ private $a; private $b; /** * Сложные команды могут принимать один или несколько объектов-получателей * вместе с любыми данными о контексте через конструктор. */ public function __construct(Receiver $receiver, string $a, string $b) { $this->receiver = $receiver; $this->a = $a; $this->b = $b; } /** * Команды могут делегировать выполнение любым методам получателя. */ public function execute(): void { echo "ComplexCommand: Complex stuff should be done by a receiver object.\n"; $this->receiver->doSomething($this->a); $this->receiver->doSomethingElse($this->b); } } /** * Классы Получателей содержат некую важную бизнес-логику. Они умеют выполнять * все виды операций, связанных с выполнением запроса. Фактически, любой класс * может выступать Получателем. */ class Receiver { public function doSomething(string $a): void { echo "Receiver: Working on (" . $a . ".)\n"; } public function doSomethingElse(string $b): void { echo "Receiver: Also working on (" . $b . ".)\n"; } } /** * Отправитель связан с одной или несколькими командами. Он отправляет запрос * команде. */ class Invoker { /** * @var Command */ private $onStart; /** * @var Command */ private $onFinish; /** * Инициализация команд. */ public function setOnStart(Command $command): void { $this->onStart = $command; } public function setOnFinish(Command $command): void { $this->onFinish = $command; } /** * Отправитель не зависит от классов конкретных команд и получателей. * Отправитель передаёт запрос получателю косвенно, выполняя команду. */ public function doSomethingImportant(): void { echo "Invoker: Does anybody want something done before I begin?\n"; if ($this->onStart instanceof Command) { $this->onStart->execute(); } echo "Invoker: ...doing something really important...\n"; echo "Invoker: Does anybody want something done after I finish?\n"; if ($this->onFinish instanceof Command) { $this->onFinish->execute(); } } } /** * Клиентский код может параметризовать отправителя любыми командами. */ $invoker = new Invoker(); $invoker->setOnStart(new SimpleCommand("Say Hi!")); $receiver = new Receiver(); $invoker->setOnFinish(new ComplexCommand($receiver, "Send email", "Save report")); $invoker->doSomethingImportant();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8t50t
function name:  (null)
number of ops:  25
compiled vars:  !0 = $invoker, !1 = $receiver
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   DECLARE_CLASS                                            'refactoringguru%5Ccommand%5Cconceptual%5Csimplecommand'
   35     1        DECLARE_CLASS                                            'refactoringguru%5Ccommand%5Cconceptual%5Ccomplexcommand'
  141     2        NEW                                              $2      'RefactoringGuru%5CCommand%5CConceptual%5CInvoker'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $2
  142     5        INIT_METHOD_CALL                                         !0, 'setOnStart'
          6        NEW                                              $5      'RefactoringGuru%5CCommand%5CConceptual%5CSimpleCommand'
          7        SEND_VAL_EX                                              'Say+Hi%21'
          8        DO_FCALL                                      0          
          9        SEND_VAR_NO_REF_EX                                       $5
         10        DO_FCALL                                      0          
  143    11        NEW                                              $8      'RefactoringGuru%5CCommand%5CConceptual%5CReceiver'
         12        DO_FCALL                                      0          
         13        ASSIGN                                                   !1, $8
  144    14        INIT_METHOD_CALL                                         !0, 'setOnFinish'
         15        NEW                                              $11     'RefactoringGuru%5CCommand%5CConceptual%5CComplexCommand'
         16        SEND_VAR_EX                                              !1
         17        SEND_VAL_EX                                              'Send+email'
         18        SEND_VAL_EX                                              'Save+report'
         19        DO_FCALL                                      0          
         20        SEND_VAR_NO_REF_EX                                       $11
         21        DO_FCALL                                      0          
  146    22        INIT_METHOD_CALL                                         !0, 'doSomethingImportant'
         23        DO_FCALL                                      0          
         24      > RETURN                                                   1

Class RefactoringGuru\Command\Conceptual\Command:
Function execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8t50t
function name:  execute
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E > > RETURN                                                   null

End of function execute

End of class RefactoringGuru\Command\Conceptual\Command.

Class RefactoringGuru\Command\Conceptual\SimpleCommand:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8t50t
function name:  __construct
number of ops:  4
compiled vars:  !0 = $payload
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
   22     1        ASSIGN_OBJ                                               'payload'
          2        OP_DATA                                                  !0
   23     3      > RETURN                                                   null

End of function __construct

Function execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8t50t
function name:  execute
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   FETCH_OBJ_R                                      ~0      'payload'
          1        CONCAT                                           ~1      'SimpleCommand%3A+See%2C+I+can+do+simple+things+like+printing+%28', ~0
          2        CONCAT                                           ~2      ~1, '%29%0A'
          3        ECHO                                                     ~2
   28     4      > RETURN                                                   null

End of function execute

End of class RefactoringGuru\Command\Conceptual\SimpleCommand.

Class RefactoringGuru\Command\Conceptual\ComplexCommand:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8t50t
function name:  __construct
number of ops:  10
compiled vars:  !0 = $receiver, !1 = $a, !2 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   55     3        ASSIGN_OBJ                                               'receiver'
          4        OP_DATA                                                  !0
   56     5        ASSIGN_OBJ                                               'a'
          6        OP_DATA                                                  !1
   57     7        ASSIGN_OBJ                                               'b'
          8        OP_DATA                                                  !2
   58     9      > RETURN                                                   null

End of function __construct

Function execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8t50t
function name:  execute
number of ops:  14
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   ECHO                                                     'ComplexCommand%3A+Complex+stuff+should+be+done+by+a+receiver+object.%0A'
   66     1        FETCH_OBJ_R                                      ~0      'receiver'
          2        INIT_METHOD_CALL                                         ~0, 'doSomething'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $1      'a'
          5        SEND_FUNC_ARG                                            $1
          6        DO_FCALL                                      0          
   67     7        FETCH_OBJ_R                                      ~3      'receiver'
          8        INIT_METHOD_CALL                                         ~3, 'doSomethingElse'
          9        CHECK_FUNC_ARG                                           
         10        FETCH_OBJ_FUNC_ARG                               $4      'b'
         11        SEND_FUNC_ARG                                            $4
         12        DO_FCALL                                      0          
   68    13      > RETURN                                                   null

End of function execute

End of class RefactoringGuru\Command\Conceptual\ComplexCommand.

Class RefactoringGuru\Command\Conceptual\Receiver:
Function dosomething:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8t50t
function name:  doSomething
number of ops:  5
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   RECV                                             !0      
   80     1        CONCAT                                           ~1      'Receiver%3A+Working+on+%28', !0
          2        CONCAT                                           ~2      ~1, '.%29%0A'
          3        ECHO                                                     ~2
   81     4      > RETURN                                                   null

End of function dosomething

Function dosomethingelse:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8t50t
function name:  doSomethingElse
number of ops:  5
compiled vars:  !0 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   RECV                                             !0      
   85     1        CONCAT                                           ~1      'Receiver%3A+Also+working+on+%28', !0
          2        CONCAT                                           ~2      ~1, '.%29%0A'
          3        ECHO                                                     ~2
   86     4      > RETURN                                                   null

End of function dosomethingelse

End of class RefactoringGuru\Command\Conceptual\Receiver.

Class RefactoringGuru\Command\Conceptual\Invoker:
Function setonstart:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8t50t
function name:  setOnStart
number of ops:  4
compiled vars:  !0 = $command
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  108     0  E >   RECV                                             !0      
  110     1        ASSIGN_OBJ                                               'onStart'
          2        OP_DATA                                                  !0
  111     3      > RETURN                                                   null

End of function setonstart

Function setonfinish:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8t50t
function name:  setOnFinish
number of ops:  4
compiled vars:  !0 = $command
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  113     0  E >   RECV                                             !0      
  115     1        ASSIGN_OBJ                                               'onFinish'
          2        OP_DATA                                                  !0
  116     3      > RETURN                                                   null

End of function setonfinish

Function dosomethingimportant:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 7
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 15
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
Branch analysis from position: 7
filename:       /in/8t50t
function name:  doSomethingImportant
number of ops:  16
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  124     0  E >   ECHO                                                     'Invoker%3A+Does+anybody+want+something+done+before+I+begin%3F%0A'
  125     1        FETCH_OBJ_R                                      ~0      'onStart'
          2        INSTANCEOF                                               ~0, 'RefactoringGuru%5CCommand%5CConceptual%5CCommand'
          3      > JMPZ                                                     ~1, ->7
  126     4    >   FETCH_OBJ_R                                      ~2      'onStart'
          5        INIT_METHOD_CALL                                         ~2, 'execute'
          6        DO_FCALL                                      0          
  129     7    >   ECHO                                                     'Invoker%3A+...doing+something+really+important...%0A'
  131     8        ECHO                                                     'Invoker%3A+Does+anybody+want+something+done+after+I+finish%3F%0A'
  132     9        FETCH_OBJ_R                                      ~4      'onFinish'
         10        INSTANCEOF                                               ~4, 'RefactoringGuru%5CCommand%5CConceptual%5CCommand'
         11      > JMPZ                                                     ~5, ->15
  133    12    >   FETCH_OBJ_R                                      ~6      'onFinish'
         13        INIT_METHOD_CALL                                         ~6, 'execute'
         14        DO_FCALL                                      0          
  135    15    > > RETURN                                                   null

End of function dosomethingimportant

End of class RefactoringGuru\Command\Conceptual\Invoker.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.53 ms | 1407 KiB | 13 Q