3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace RefactoringGuru\Proxy\RealWorld; /** * Интерфейс Субъекта описывает интерфейс реального объекта. * * Дело в том, что у большинства приложений нет чётко определённого интерфейса. * В этом случае лучше было бы расширить Заместителя за счёт существующего * класса приложения. Если это неудобно, тогда первым шагом должно быть * извлечение правильного интерфейса. */ interface Downloader { public function download(string $url): string; } /** * Реальный Субъект делает реальную работу, хотя и не самым эффективным * способом. Когда клиент пытается загрузить тот же самый файл во второй раз, * наш загрузчик именно это и делает, вместо того, чтобы извлечь результат из * кэша. */ class SimpleDownloader implements Downloader { public function download(string $url): string { echo "Downloading a file from the Internet.\n"; $result = file_get_contents($url); echo "Downloaded bytes: " . strlen($result) . "\n"; return $result; } } /** * Класс Заместителя – это попытка сделать загрузку более эффективной. Он * обёртывает реальный объект загрузчика и делегирует ему первые запросы на * скачивание. Затем результат кэшируется, что позволяет последующим вызовам * возвращать уже имеющийся файл вместо его повторной загрузки. */ class CachingDownloader implements Downloader { /** * @var SimpleDownloader */ private $downloader; /** * @var string[] */ private $cache = []; public function __construct(SimpleDownloader $downloader) { $this->downloader = $downloader; } public function download(string $url): string { if (!isset($this->cache[$url])) { echo "CacheProxy MISS. "; $result = $this->downloader->download($url); $this->cache[$url] = $result; } else { echo "CacheProxy HIT. Retrieving result from cache.\n"; } return $this->cache[$url]; } } /** * Клиентский код может выдать несколько похожих запросов на загрузку. В этом * случае кэширующий заместитель экономит время и трафик, подавая результаты из * кэша. * * Клиент не знает, что он работает с заместителем, потому что он работает с * загрузчиками через абстрактный интерфейс. */ function clientCode(Downloader $subject) { // ... $result = $subject->download("http://example.com/"); // Повторяющиеся запросы на загрузку могут кэшироваться для увеличения // скорости. $result = $subject->download("http://example.com/"); // ... } echo "Executing client code with real subject:\n"; $realSubject = new SimpleDownloader(); clientCode($realSubject); echo "\n"; echo "Executing the same client code with a proxy:\n"; $proxy = new CachingDownloader($realSubject); clientCode($proxy);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6uCRG
function name:  (null)
number of ops:  19
compiled vars:  !0 = $realSubject, !1 = $proxy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   DECLARE_CLASS                                            'refactoringguru%5Cproxy%5Crealworld%5Csimpledownloader'
   42     1        DECLARE_CLASS                                            'refactoringguru%5Cproxy%5Crealworld%5Ccachingdownloader'
   94     2        ECHO                                                     'Executing+client+code+with+real+subject%3A%0A'
   95     3        NEW                                              $2      'RefactoringGuru%5CProxy%5CRealWorld%5CSimpleDownloader'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $2
   96     6        INIT_NS_FCALL_BY_NAME                                    'RefactoringGuru%5CProxy%5CRealWorld%5CclientCode'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0          
   98     9        ECHO                                                     '%0A'
  100    10        ECHO                                                     'Executing+the+same+client+code+with+a+proxy%3A%0A'
  101    11        NEW                                              $6      'RefactoringGuru%5CProxy%5CRealWorld%5CCachingDownloader'
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0          
         14        ASSIGN                                                   !1, $6
  102    15        INIT_NS_FCALL_BY_NAME                                    'RefactoringGuru%5CProxy%5CRealWorld%5CclientCode'
         16        SEND_VAR_EX                                              !1
         17        DO_FCALL                                      0          
         18      > RETURN                                                   1

Function refactoringguru%5Cproxy%5Crealworld%5Cclientcode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6uCRG
function name:  RefactoringGuru\Proxy\RealWorld\clientCode
number of ops:  10
compiled vars:  !0 = $subject, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   RECV                                             !0      
   84     1        INIT_METHOD_CALL                                         !0, 'download'
          2        SEND_VAL_EX                                              'http%3A%2F%2Fexample.com%2F'
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !1, $2
   89     5        INIT_METHOD_CALL                                         !0, 'download'
          6        SEND_VAL_EX                                              'http%3A%2F%2Fexample.com%2F'
          7        DO_FCALL                                      0  $4      
          8        ASSIGN                                                   !1, $4
   92     9      > RETURN                                                   null

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

Class RefactoringGuru\Proxy\RealWorld\Downloader:
Function download:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6uCRG
function name:  download
number of ops:  3
compiled vars:  !0 = $url
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        VERIFY_RETURN_TYPE                                       
          2      > RETURN                                                   null

End of function download

End of class RefactoringGuru\Proxy\RealWorld\Downloader.

Class RefactoringGuru\Proxy\RealWorld\SimpleDownloader:
Function download:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6uCRG
function name:  download
number of ops:  16
compiled vars:  !0 = $url, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
   28     1        ECHO                                                     'Downloading+a+file+from+the+Internet.%0A'
   29     2        INIT_NS_FCALL_BY_NAME                                    'RefactoringGuru%5CProxy%5CRealWorld%5Cfile_get_contents'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $2      
          5        ASSIGN                                                   !1, $2
   30     6        INIT_NS_FCALL_BY_NAME                                    'RefactoringGuru%5CProxy%5CRealWorld%5Cstrlen'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0  $4      
          9        CONCAT                                           ~5      'Downloaded+bytes%3A+', $4
         10        CONCAT                                           ~6      ~5, '%0A'
         11        ECHO                                                     ~6
   32    12        VERIFY_RETURN_TYPE                                       !1
         13      > RETURN                                                   !1
   33    14*       VERIFY_RETURN_TYPE                                       
         15*     > RETURN                                                   null

End of function download

End of class RefactoringGuru\Proxy\RealWorld\SimpleDownloader.

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

End of function __construct

Function download:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 15
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6uCRG
function name:  download
number of ops:  22
compiled vars:  !0 = $url, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
   61     1        FETCH_OBJ_IS                                     ~2      'cache'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~3      ~2, !0
          3        BOOL_NOT                                         ~4      ~3
          4      > JMPZ                                                     ~4, ->15
   62     5    >   ECHO                                                     'CacheProxy+MISS.+'
   63     6        FETCH_OBJ_R                                      ~5      'downloader'
          7        INIT_METHOD_CALL                                         ~5, 'download'
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0  $6      
         10        ASSIGN                                                   !1, $6
   64    11        FETCH_OBJ_W                                      $8      'cache'
         12        ASSIGN_DIM                                               $8, !0
         13        OP_DATA                                                  !1
         14      > JMP                                                      ->16
   66    15    >   ECHO                                                     'CacheProxy+HIT.+Retrieving+result+from+cache.%0A'
   68    16    >   FETCH_OBJ_R                                      ~10     'cache'
         17        FETCH_DIM_R                                      ~11     ~10, !0
         18        VERIFY_RETURN_TYPE                                       ~11
         19      > RETURN                                                   ~11
   69    20*       VERIFY_RETURN_TYPE                                       
         21*     > RETURN                                                   null

End of function download

End of class RefactoringGuru\Proxy\RealWorld\CachingDownloader.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.08 ms | 1407 KiB | 19 Q