3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace RefactoringGuru\AbstractFactory\Conceptual; /** * Паттерн Абстрактная Фабрика * * Назначение: Предоставляет интерфейс для создания семейств связанных или * зависимых объектов без привязки к их конкретным классам. */ /** * Интерфейс Абстрактной Фабрики объявляет набор методов, которые возвращают * различные абстрактные продукты. Эти продукты называются семейством и связаны * темой или концепцией высокого уровня. Продукты одного семейства обычно могут * взаимодействовать между собой. Семейство продуктов может иметь несколько * вариаций, но продукты одной вариации несовместимы с продуктами другой. */ interface AbstractFactory { public function createProductA(): AbstractProductA; public function createProductB(): AbstractProductB; } /** * Конкретная Фабрика производит семейство продуктов одной вариации. Фабрика * гарантирует совместимость полученных продуктов. Обратите внимание, что * сигнатуры методов Конкретной Фабрики возвращают абстрактный продукт, в то * время как внутри метода создается экземпляр конкретного продукта. */ class ConcreteFactory1 implements AbstractFactory { public function createProductA(): AbstractProductA { return new ConcreteProductA1(); } public function createProductB(): AbstractProductB { return new ConcreteProductB1(); } } /** * Каждая Конкретная Фабрика имеет соответствующую вариацию продукта. */ class ConcreteFactory2 implements AbstractFactory { public function createProductA(): AbstractProductA { return new ConcreteProductA2(); } public function createProductB(): AbstractProductB { return new ConcreteProductB2(); } } /** * Каждый отдельный продукт семейства продуктов должен иметь базовый интерфейс. * Все вариации продукта должны реализовывать этот интерфейс. */ interface AbstractProductA { public function usefulFunctionA(): string; } /** * Конкретные продукты создаются соответствующими Конкретными Фабриками. */ class ConcreteProductA1 implements AbstractProductA { public function usefulFunctionA(): string { return "The result of the product A1."; } } class ConcreteProductA2 implements AbstractProductA { public function usefulFunctionA(): string { return "The result of the product A2."; } } /** * Базовый интерфейс другого продукта. Все продукты могут взаимодействовать друг * с другом, но правильное взаимодействие возможно только между продуктами одной * и той же конкретной вариации. */ interface AbstractProductB { /** * Продукт B способен работать самостоятельно... */ public function usefulFunctionB(): string; /** * ...а также взаимодействовать с Продуктами A той же вариации. * * Абстрактная Фабрика гарантирует, что все продукты, которые она создает, * имеют одинаковую вариацию и, следовательно, совместимы. */ public function anotherUsefulFunctionB(AbstractProductA $collaborator): string; } /** * Конкретные Продукты создаются соответствующими Конкретными Фабриками. */ class ConcreteProductB1 implements AbstractProductB { public function usefulFunctionB(): string { return "The result of the product B1."; } /** * Продукт B1 может корректно работать только с Продуктом A1. Тем не менее, * он принимает любой экземпляр Абстрактного Продукта А в качестве * аргумента. */ public function anotherUsefulFunctionB(AbstractProductA $collaborator): string { $result = $collaborator->usefulFunctionA(); return "The result of the B1 collaborating with the ({$result})"; } } class ConcreteProductB2 implements AbstractProductB { public function usefulFunctionB(): string { return "The result of the product B2."; } /** * Продукт B2 может корректно работать только с Продуктом A2. Тем не менее, * он принимает любой экземпляр Абстрактного Продукта А в качестве * аргумента. */ public function anotherUsefulFunctionB(AbstractProductA $collaborator): string { $result = $collaborator->usefulFunctionA(); return "The result of the B2 collaborating with the ({$result})"; } } /** * Клиентский код работает с фабриками и продуктами только через абстрактные * типы: Абстрактная Фабрика и Абстрактный Продукт. Это позволяет передавать * любой подкласс фабрики или продукта клиентскому коду, не нарушая его. */ function clientCode(AbstractFactory $factory) { $productA = $factory->createProductA(); $productB = $factory->createProductB(); echo $productB->usefulFunctionB() . "\n"; echo $productB->anotherUsefulFunctionB($productA) . "\n"; } /** * Клиентский код может работать с любым конкретным классом фабрики. */ echo "Client: Testing client code with the first factory type:\n"; clientCode(new ConcreteFactory1()); echo "\n"; echo "Client: Testing the same client code with the second factory type:\n"; clientCode(new ConcreteFactory2());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  (null)
number of ops:  20
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   DECLARE_CLASS                                            'refactoringguru%5Cabstractfactory%5Cconceptual%5Cconcretefactory1'
   48     1        DECLARE_CLASS                                            'refactoringguru%5Cabstractfactory%5Cconceptual%5Cconcretefactory2'
   73     2        DECLARE_CLASS                                            'refactoringguru%5Cabstractfactory%5Cconceptual%5Cconcreteproducta1'
   81     3        DECLARE_CLASS                                            'refactoringguru%5Cabstractfactory%5Cconceptual%5Cconcreteproducta2'
  113     4        DECLARE_CLASS                                            'refactoringguru%5Cabstractfactory%5Cconceptual%5Cconcreteproductb1'
  133     5        DECLARE_CLASS                                            'refactoringguru%5Cabstractfactory%5Cconceptual%5Cconcreteproductb2'
  170     6        ECHO                                                     'Client%3A+Testing+client+code+with+the+first+factory+type%3A%0A'
  171     7        INIT_NS_FCALL_BY_NAME                                    'RefactoringGuru%5CAbstractFactory%5CConceptual%5CclientCode'
          8        NEW                                              $0      'RefactoringGuru%5CAbstractFactory%5CConceptual%5CConcreteFactory1'
          9        DO_FCALL                                      0          
         10        SEND_VAR_NO_REF_EX                                       $0
         11        DO_FCALL                                      0          
  173    12        ECHO                                                     '%0A'
  175    13        ECHO                                                     'Client%3A+Testing+the+same+client+code+with+the+second+factory+type%3A%0A'
  176    14        INIT_NS_FCALL_BY_NAME                                    'RefactoringGuru%5CAbstractFactory%5CConceptual%5CclientCode'
         15        NEW                                              $3      'RefactoringGuru%5CAbstractFactory%5CConceptual%5CConcreteFactory2'
         16        DO_FCALL                                      0          
         17        SEND_VAR_NO_REF_EX                                       $3
         18        DO_FCALL                                      0          
         19      > RETURN                                                   1

Function refactoringguru%5Cabstractfactory%5Cconceptual%5Cclientcode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  RefactoringGuru\AbstractFactory\Conceptual\clientCode
number of ops:  17
compiled vars:  !0 = $factory, !1 = $productA, !2 = $productB
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  158     0  E >   RECV                                             !0      
  160     1        INIT_METHOD_CALL                                         !0, 'createProductA'
          2        DO_FCALL                                      0  $3      
          3        ASSIGN                                                   !1, $3
  161     4        INIT_METHOD_CALL                                         !0, 'createProductB'
          5        DO_FCALL                                      0  $5      
          6        ASSIGN                                                   !2, $5
  163     7        INIT_METHOD_CALL                                         !2, 'usefulFunctionB'
          8        DO_FCALL                                      0  $7      
          9        CONCAT                                           ~8      $7, '%0A'
         10        ECHO                                                     ~8
  164    11        INIT_METHOD_CALL                                         !2, 'anotherUsefulFunctionB'
         12        SEND_VAR_EX                                              !1
         13        DO_FCALL                                      0  $9      
         14        CONCAT                                           ~10     $9, '%0A'
         15        ECHO                                                     ~10
  165    16      > RETURN                                                   null

End of function refactoringguru%5Cabstractfactory%5Cconceptual%5Cclientcode

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

End of function createproducta

Function createproductb:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  createProductB
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   VERIFY_RETURN_TYPE                                       
          1      > RETURN                                                   null

End of function createproductb

End of class RefactoringGuru\AbstractFactory\Conceptual\AbstractFactory.

Class RefactoringGuru\AbstractFactory\Conceptual\ConcreteFactory1:
Function createproducta:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  createProductA
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   NEW                                              $0      'RefactoringGuru%5CAbstractFactory%5CConceptual%5CConcreteProductA1'
          1        DO_FCALL                                      0          
          2        VERIFY_RETURN_TYPE                                       $0
          3      > RETURN                                                   $0
   37     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function createproducta

Function createproductb:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  createProductB
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   NEW                                              $0      'RefactoringGuru%5CAbstractFactory%5CConceptual%5CConcreteProductB1'
          1        DO_FCALL                                      0          
          2        VERIFY_RETURN_TYPE                                       $0
          3      > RETURN                                                   $0
   42     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function createproductb

End of class RefactoringGuru\AbstractFactory\Conceptual\ConcreteFactory1.

Class RefactoringGuru\AbstractFactory\Conceptual\ConcreteFactory2:
Function createproducta:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  createProductA
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   NEW                                              $0      'RefactoringGuru%5CAbstractFactory%5CConceptual%5CConcreteProductA2'
          1        DO_FCALL                                      0          
          2        VERIFY_RETURN_TYPE                                       $0
          3      > RETURN                                                   $0
   53     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function createproducta

Function createproductb:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  createProductB
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   NEW                                              $0      'RefactoringGuru%5CAbstractFactory%5CConceptual%5CConcreteProductB2'
          1        DO_FCALL                                      0          
          2        VERIFY_RETURN_TYPE                                       $0
          3      > RETURN                                                   $0
   58     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function createproductb

End of class RefactoringGuru\AbstractFactory\Conceptual\ConcreteFactory2.

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

End of function usefulfunctiona

End of class RefactoringGuru\AbstractFactory\Conceptual\AbstractProductA.

Class RefactoringGuru\AbstractFactory\Conceptual\ConcreteProductA1:
Function usefulfunctiona:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  usefulFunctionA
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E > > RETURN                                                   'The+result+of+the+product+A1.'
   78     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function usefulfunctiona

End of class RefactoringGuru\AbstractFactory\Conceptual\ConcreteProductA1.

Class RefactoringGuru\AbstractFactory\Conceptual\ConcreteProductA2:
Function usefulfunctiona:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  usefulFunctionA
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E > > RETURN                                                   'The+result+of+the+product+A2.'
   86     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function usefulfunctiona

End of class RefactoringGuru\AbstractFactory\Conceptual\ConcreteProductA2.

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

End of function usefulfunctionb

Function anotherusefulfunctionb:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  anotherUsefulFunctionB
number of ops:  3
compiled vars:  !0 = $collaborator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  107     0  E >   RECV                                             !0      
          1        VERIFY_RETURN_TYPE                                       
          2      > RETURN                                                   null

End of function anotherusefulfunctionb

End of class RefactoringGuru\AbstractFactory\Conceptual\AbstractProductB.

Class RefactoringGuru\AbstractFactory\Conceptual\ConcreteProductB1:
Function usefulfunctionb:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  usefulFunctionB
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  117     0  E > > RETURN                                                   'The+result+of+the+product+B1.'
  118     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function usefulfunctionb

Function anotherusefulfunctionb:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  anotherUsefulFunctionB
number of ops:  11
compiled vars:  !0 = $collaborator, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  125     0  E >   RECV                                             !0      
  127     1        INIT_METHOD_CALL                                         !0, 'usefulFunctionA'
          2        DO_FCALL                                      0  $2      
          3        ASSIGN                                                   !1, $2
  129     4        ROPE_INIT                                     3  ~5      'The+result+of+the+B1+collaborating+with+the+%28'
          5        ROPE_ADD                                      1  ~5      ~5, !1
          6        ROPE_END                                      2  ~4      ~5, '%29'
          7        VERIFY_RETURN_TYPE                                       ~4
          8      > RETURN                                                   ~4
  130     9*       VERIFY_RETURN_TYPE                                       
         10*     > RETURN                                                   null

End of function anotherusefulfunctionb

End of class RefactoringGuru\AbstractFactory\Conceptual\ConcreteProductB1.

Class RefactoringGuru\AbstractFactory\Conceptual\ConcreteProductB2:
Function usefulfunctionb:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  usefulFunctionB
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  137     0  E > > RETURN                                                   'The+result+of+the+product+B2.'
  138     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function usefulfunctionb

Function anotherusefulfunctionb:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDeR3
function name:  anotherUsefulFunctionB
number of ops:  11
compiled vars:  !0 = $collaborator, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  145     0  E >   RECV                                             !0      
  147     1        INIT_METHOD_CALL                                         !0, 'usefulFunctionA'
          2        DO_FCALL                                      0  $2      
          3        ASSIGN                                                   !1, $2
  149     4        ROPE_INIT                                     3  ~5      'The+result+of+the+B2+collaborating+with+the+%28'
          5        ROPE_ADD                                      1  ~5      ~5, !1
          6        ROPE_END                                      2  ~4      ~5, '%29'
          7        VERIFY_RETURN_TYPE                                       ~4
          8      > RETURN                                                   ~4
  150     9*       VERIFY_RETURN_TYPE                                       
         10*     > RETURN                                                   null

End of function anotherusefulfunctionb

End of class RefactoringGuru\AbstractFactory\Conceptual\ConcreteProductB2.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
178.41 ms | 1415 KiB | 15 Q