3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface ProductRepository { public function findReferenceBySku(string $sku): Product; public function findIdBySku(string $sku): int; } class Product { public function __construct( public int $id, ) { } } class ProductDatabaseRepository implements ProductRepository { public function findReferenceBySku(string $sku): Product { $id = $this->findIdBySku($sku); return $this->getProductReference($id); } private function getProductReference(int $id) { // check entity manager for existing object // and generate proxy if not found return new Product($id); } public function findIdBySku(string $sku): int { $this->queryDatabase(); return match($sku) { 'sku1' => 1, }; } protected function queryDatabase() { // note this method is not public var_dump('query database'); } } class ProductCachedRepository implements ProductRepository { private array $cachedIds = []; public function __construct( private ProductRepository $inner ) {} public function findReferenceBySku(string $sku): Product { // not able to delegate this logic directly to inner class // (loss of findIdBySku() overridden logic) //return $this->inner->findReferenceBySku($sku); // as well not able call decorated method with substitude this return $this->inner->findReferenceBySku(...)->call($this, $sku); } public function findIdBySku(string $sku): int { var_dump('check cache'); return $this->cachedIds[$sku] ??= $this->inner->findIdBySku($sku); } public function __call($name, $arguments) { // all not public methods of inner repository // which are not declared in current class // are delegated to inner repository return (fn() => $this->$name(...$arguments)) ->call($this->inner); } } $repository = (new ProductCachedRepository(new ProductDatabaseRepository())); var_dump('call1', $repository->findReferenceBySku('sku1')); var_dump('call2', $repository->findReferenceBySku('sku1')); // cache hit expected var_dump('call2', $repository->findReferenceBySku('sku1')); // cache hit expected
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CFE1u
function name:  (null)
number of ops:  30
compiled vars:  !0 = $repository
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   DECLARE_CLASS                                            'productdatabaserepository'
   52     1        DECLARE_CLASS                                            'productcachedrepository'
   90     2        NEW                                              $1      'ProductCachedRepository'
          3        NEW                                              $2      'ProductDatabaseRepository'
          4        DO_FCALL                                      0          
          5        SEND_VAR_NO_REF_EX                                       $2
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !0, $1
   92     8        INIT_FCALL                                               'var_dump'
          9        SEND_VAL                                                 'call1'
         10        INIT_METHOD_CALL                                         !0, 'findReferenceBySku'
         11        SEND_VAL_EX                                              'sku1'
         12        DO_FCALL                                      0  $6      
         13        SEND_VAR                                                 $6
         14        DO_ICALL                                                 
   93    15        INIT_FCALL                                               'var_dump'
         16        SEND_VAL                                                 'call2'
         17        INIT_METHOD_CALL                                         !0, 'findReferenceBySku'
         18        SEND_VAL_EX                                              'sku1'
         19        DO_FCALL                                      0  $8      
         20        SEND_VAR                                                 $8
         21        DO_ICALL                                                 
   94    22        INIT_FCALL                                               'var_dump'
         23        SEND_VAL                                                 'call2'
         24        INIT_METHOD_CALL                                         !0, 'findReferenceBySku'
         25        SEND_VAL_EX                                              'sku1'
         26        DO_FCALL                                      0  $10     
         27        SEND_VAR                                                 $10
         28        DO_ICALL                                                 
         29      > RETURN                                                   1

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

End of function findreferencebysku

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

End of function findidbysku

End of class ProductRepository.

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

End of function __construct

End of class Product.

Class ProductDatabaseRepository:
Function findreferencebysku:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CFE1u
function name:  findReferenceBySku
number of ops:  12
compiled vars:  !0 = $sku, !1 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   21     1        INIT_METHOD_CALL                                         'findIdBySku'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !1, $2
   23     5        INIT_METHOD_CALL                                         'getProductReference'
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0  $4      
          8        VERIFY_RETURN_TYPE                                       $4
          9      > RETURN                                                   $4
   24    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null

End of function findreferencebysku

Function getproductreference:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CFE1u
function name:  getProductReference
number of ops:  6
compiled vars:  !0 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
   31     1        NEW                                              $1      'Product'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4      > RETURN                                                   $1
   32     5*     > RETURN                                                   null

End of function getproductreference

Function findidbysku:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
1 jumps found. (Code = 197) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CFE1u
function name:  findIdBySku
number of ops:  13
compiled vars:  !0 = $sku
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   36     1        INIT_METHOD_CALL                                         'queryDatabase'
          2        DO_FCALL                                      0          
   39     3        IS_IDENTICAL                                             !0, 'sku1'
          4      > JMPNZ                                                    ~2, ->7
          5    > > JMP                                                      ->6
          6    > > MATCH_ERROR                                              !0
          7    >   QM_ASSIGN                                        ~3      1
          8      > JMP                                                      ->9
          9    >   VERIFY_RETURN_TYPE                                       ~3
         10      > RETURN                                                   ~3
   41    11*       VERIFY_RETURN_TYPE                                       
         12*     > RETURN                                                   null

End of function findidbysku

Function querydatabase:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CFE1u
function name:  queryDatabase
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'query+database'
          2        DO_ICALL                                                 
   48     3      > RETURN                                                   null

End of function querydatabase

End of class ProductDatabaseRepository.

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

End of function __construct

Function findreferencebysku:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CFE1u
function name:  findReferenceBySku
number of ops:  13
compiled vars:  !0 = $sku
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
   67     1        FETCH_OBJ_R                                      ~1      'inner'
          2        INIT_METHOD_CALL                                         ~1, 'findReferenceBySku'
          3        ZEND_CALLABLE_CONVERT                            ~2      
          4        INIT_METHOD_CALL                                         ~2, 'call'
          5        FETCH_THIS                                       $3      
          6        SEND_VAR_EX                                              $3
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0  $4      
          9        VERIFY_RETURN_TYPE                                       $4
         10      > RETURN                                                   $4
   68    11*       VERIFY_RETURN_TYPE                                       
         12*     > RETURN                                                   null

End of function findreferencebysku

Function findidbysku:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CFE1u
function name:  findIdBySku
number of ops:  19
compiled vars:  !0 = $sku
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   RECV                                             !0      
   72     1        INIT_FCALL                                               'var_dump'
          2        SEND_VAL                                                 'check+cache'
          3        DO_ICALL                                                 
   74     4        FETCH_OBJ_IS                                     ~2      'cachedIds'
          5        FETCH_DIM_IS                                     ~3      ~2, !0
          6        COALESCE                                         ~4      ~3
   75     7        FETCH_OBJ_R                                      ~5      'inner'
          8        INIT_METHOD_CALL                                         ~5, 'findIdBySku'
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0  $6      
   74    11        FETCH_OBJ_W                                      $7      'cachedIds'
         12        ASSIGN_DIM                                       ~8      $7, !0
         13        OP_DATA                                                  $6
         14        QM_ASSIGN                                        ~4      ~8
         15        VERIFY_RETURN_TYPE                                       ~4
         16      > RETURN                                                   ~4
   76    17*       VERIFY_RETURN_TYPE                                       
         18*     > RETURN                                                   null

End of function findidbysku

Function __call:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CFE1u
function name:  __call
number of ops:  12
compiled vars:  !0 = $name, !1 = $arguments
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   84     2        DECLARE_LAMBDA_FUNCTION                          ~2      [0]
          3        BIND_LEXICAL                                             ~2, !0
          4        BIND_LEXICAL                                             ~2, !1
   85     5        INIT_METHOD_CALL                                         ~2, 'call'
          6        CHECK_FUNC_ARG                                           
          7        FETCH_OBJ_FUNC_ARG                               $3      'inner'
          8        SEND_FUNC_ARG                                            $3
          9        DO_FCALL                                      0  $4      
         10      > RETURN                                                   $4
   86    11*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CFE1u
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $name, !1 = $arguments
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
          2        FETCH_THIS                                       $2      
          3        INIT_METHOD_CALL                                         $2, !0
          4        SEND_UNPACK                                              !1
          5        CHECK_UNDEF_ARGS                                         
          6        DO_FCALL                                      1  $3      
          7      > RETURN                                                   $3
          8*     > RETURN                                                   null

End of Dynamic Function 0

End of function __call

End of class ProductCachedRepository.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.78 ms | 1014 KiB | 14 Q