3v4l.org

run code in 300+ PHP versions simultaneously
<?php final readonly class Product { public function __construct( public string $id, public string $name, public string $description, public int $price = 10, ) { } } final class Cart { /** * @param Product[] $products */ public function __construct( public string $id, public array $products = [], public int $total = 0, public int $quantity = 0, ) { } public function addProduct(Product $product): void { $this->products[] = $product; $this->quantity++; $this->total += $product->price; } } final readonly class ProductRepository { public function find(string $id): Product { // Simulate loading from database return new Product($id, "product-$id", "description-$id"); } public function findByIds(array $ids): array { // Simulate loading from database $products = []; foreach ($ids as $id) { $products[] = $this->find($id); } return $products; } } final readonly class CartRepository { private ReflectionClass $reflector; public function __construct( private ProductRepository $productRepository, ) { $this->reflector = new ReflectionClass(Cart::class); } public function serialize(Cart $cart): string { return json_encode([ 'id' => $cart->id, 'products' => array_column($cart->products, 'id'), 'total' => $cart->total, 'quantity' => $cart->quantity, ]); } public function unserialize(string $data): Cart { var_dump('...Unserializing cart...'); $previousCart = json_decode($data, true); $newCart = $this->reflector->newLazyGhost(function(Cart $newCart) use ($previousCart): void { var_dump('...Loading products...'); $products = $this->productRepository->findByIds($previousCart['products']); $this->reflector->getProperty('products')->setValue($newCart, $products); }); $this->reflector->getProperty('id')->setRawValueWithoutLazyInitialization($newCart, $previousCart['id']); $this->reflector->getProperty('total')->setRawValueWithoutLazyInitialization($newCart, $previousCart['total']); $this->reflector->getProperty('quantity')->setRawValueWithoutLazyInitialization($newCart, $previousCart['quantity']); return $newCart; } } $productRepository = new ProductRepository(); $repository = new CartRepository($productRepository); $cart = new Cart('#cart-01'); $cart->addProduct($productRepository->find('1')); $cart->addProduct($productRepository->find('2')); $cart->addProduct($productRepository->find('3')); // Usually, the $serialized string would be stored in a database or in the // session $serialized = $repository->serialize($cart); // A new HTTP request is made and the $serialized string is retrieved from the // DB or the sessions $newCart = $repository->unserialize($serialized); var_dump($newCart); var_dump($newCart->total); $newCart->products; var_dump($newCart);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1uR3Y
function name:  (null)
number of ops:  50
compiled vars:  !0 = $productRepository, !1 = $repository, !2 = $cart, !3 = $serialized, !4 = $newCart
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   94     0  E >   NEW                                              $5      'ProductRepository'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $5
   95     3        NEW                                              $8      'CartRepository'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !1, $8
   97     7        NEW                                              $11     'Cart'
          8        SEND_VAL_EX                                              '%23cart-01'
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !2, $11
   98    11        INIT_METHOD_CALL                                         !2, 'addProduct'
         12        INIT_METHOD_CALL                                         !0, 'find'
         13        SEND_VAL_EX                                              '1'
         14        DO_FCALL                                      0  $14     
         15        SEND_VAR_NO_REF_EX                                       $14
         16        DO_FCALL                                      0          
   99    17        INIT_METHOD_CALL                                         !2, 'addProduct'
         18        INIT_METHOD_CALL                                         !0, 'find'
         19        SEND_VAL_EX                                              '2'
         20        DO_FCALL                                      0  $16     
         21        SEND_VAR_NO_REF_EX                                       $16
         22        DO_FCALL                                      0          
  100    23        INIT_METHOD_CALL                                         !2, 'addProduct'
         24        INIT_METHOD_CALL                                         !0, 'find'
         25        SEND_VAL_EX                                              '3'
         26        DO_FCALL                                      0  $18     
         27        SEND_VAR_NO_REF_EX                                       $18
         28        DO_FCALL                                      0          
  104    29        INIT_METHOD_CALL                                         !1, 'serialize'
         30        SEND_VAR_EX                                              !2
         31        DO_FCALL                                      0  $20     
         32        ASSIGN                                                   !3, $20
  108    33        INIT_METHOD_CALL                                         !1, 'unserialize'
         34        SEND_VAR_EX                                              !3
         35        DO_FCALL                                      0  $22     
         36        ASSIGN                                                   !4, $22
  109    37        INIT_FCALL                                               'var_dump'
         38        SEND_VAR                                                 !4
         39        DO_ICALL                                                 
  111    40        INIT_FCALL                                               'var_dump'
         41        FETCH_OBJ_R                                      ~25     !4, 'total'
         42        SEND_VAL                                                 ~25
         43        DO_ICALL                                                 
  113    44        FETCH_OBJ_R                                      ~27     !4, 'products'
         45        FREE                                                     ~27
  115    46        INIT_FCALL                                               'var_dump'
         47        SEND_VAR                                                 !4
         48        DO_ICALL                                                 
         49      > RETURN                                                   1

Class Product:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1uR3Y
function name:  __construct
number of ops:  13
compiled vars:  !0 = $id, !1 = $name, !2 = $description, !3 = $price
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      10
          4        ASSIGN_OBJ                                               'id'
          5        OP_DATA                                                  !0
          6        ASSIGN_OBJ                                               'name'
          7        OP_DATA                                                  !1
          8        ASSIGN_OBJ                                               'description'
          9        OP_DATA                                                  !2
         10        ASSIGN_OBJ                                               'price'
         11        OP_DATA                                                  !3
   11    12      > RETURN                                                   null

End of function __construct

End of class Product.

Class Cart:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1uR3Y
function name:  __construct
number of ops:  13
compiled vars:  !0 = $id, !1 = $products, !2 = $total, !3 = $quantity
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
          2        RECV_INIT                                        !2      0
          3        RECV_INIT                                        !3      0
          4        ASSIGN_OBJ                                               'id'
          5        OP_DATA                                                  !0
          6        ASSIGN_OBJ                                               'products'
          7        OP_DATA                                                  !1
          8        ASSIGN_OBJ                                               'total'
          9        OP_DATA                                                  !2
         10        ASSIGN_OBJ                                               'quantity'
         11        OP_DATA                                                  !3
   25    12      > RETURN                                                   null

End of function __construct

Function addproduct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1uR3Y
function name:  addProduct
number of ops:  9
compiled vars:  !0 = $product
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   29     1        FETCH_OBJ_W                                      $1      'products'
          2        ASSIGN_DIM                                               $1
          3        OP_DATA                                                  !0
   30     4        PRE_INC_OBJ                                              'quantity'
   31     5        FETCH_OBJ_R                                      ~5      !0, 'price'
          6        ASSIGN_OBJ_OP                                 1          'total'
          7        OP_DATA                                                  ~5
   32     8      > RETURN                                                   null

End of function addproduct

End of class Cart.

Class ProductRepository:
Function find:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1uR3Y
function name:  find
number of ops:  14
compiled vars:  !0 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
   40     1        NEW                                              $1      'Product'
          2        SEND_VAR_EX                                              !0
          3        NOP                                                      
          4        FAST_CONCAT                                      ~2      'product-', !0
          5        SEND_VAL_EX                                              ~2
          6        NOP                                                      
          7        FAST_CONCAT                                      ~3      'description-', !0
          8        SEND_VAL_EX                                              ~3
          9        DO_FCALL                                      0          
         10        VERIFY_RETURN_TYPE                                       $1
         11      > RETURN                                                   $1
   41    12*       VERIFY_RETURN_TYPE                                       
         13*     > RETURN                                                   null

End of function find

Function findbyids:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 10
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/1uR3Y
function name:  findByIds
number of ops:  15
compiled vars:  !0 = $ids, !1 = $products, !2 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
   46     1        ASSIGN                                                   !1, <array>
   47     2      > FE_RESET_R                                       $4      !0, ->10
          3    > > FE_FETCH_R                                               $4, !2, ->10
   48     4    >   INIT_METHOD_CALL                                         'find'
          5        SEND_VAR_EX                                              !2
          6        DO_FCALL                                      0  $6      
          7        ASSIGN_DIM                                               !1
          8        OP_DATA                                                  $6
   47     9      > JMP                                                      ->3
         10    >   FE_FREE                                                  $4
   51    11        VERIFY_RETURN_TYPE                                       !1
         12      > RETURN                                                   !1
   52    13*       VERIFY_RETURN_TYPE                                       
         14*     > RETURN                                                   null

End of function findbyids

End of class ProductRepository.

Class CartRepository:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1uR3Y
function name:  __construct
number of ops:  9
compiled vars:  !0 = $productRepository
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'productRepository'
          2        OP_DATA                                                  !0
   62     3        NEW                                              $2      'ReflectionClass'
          4        SEND_VAL_EX                                              'Cart'
          5        DO_FCALL                                      0          
          6        ASSIGN_OBJ                                               'reflector'
          7        OP_DATA                                                  $2
   63     8      > RETURN                                                   null

End of function __construct

Function serialize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1uR3Y
function name:  serialize
number of ops:  20
compiled vars:  !0 = $cart
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
   67     1        INIT_FCALL                                               'json_encode'
   68     2        FETCH_OBJ_R                                      ~1      !0, 'id'
          3        INIT_ARRAY                                       ~2      ~1, 'id'
   69     4        INIT_FCALL                                               'array_column'
          5        FETCH_OBJ_R                                      ~3      !0, 'products'
          6        SEND_VAL                                                 ~3
          7        SEND_VAL                                                 'id'
          8        DO_ICALL                                         $4      
          9        ADD_ARRAY_ELEMENT                                ~2      $4, 'products'
   70    10        FETCH_OBJ_R                                      ~5      !0, 'total'
         11        ADD_ARRAY_ELEMENT                                ~2      ~5, 'total'
   71    12        FETCH_OBJ_R                                      ~6      !0, 'quantity'
         13        ADD_ARRAY_ELEMENT                                ~2      ~6, 'quantity'
         14        SEND_VAL                                                 ~2
   67    15        DO_ICALL                                         $7      
   71    16        VERIFY_RETURN_TYPE                                       $7
         17      > RETURN                                                   $7
   73    18*       VERIFY_RETURN_TYPE                                       
         19*     > RETURN                                                   null

End of function serialize

Function unserialize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1uR3Y
function name:  unserialize
number of ops:  50
compiled vars:  !0 = $data, !1 = $previousCart, !2 = $newCart
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   RECV                                             !0      
   77     1        INIT_FCALL                                               'var_dump'
          2        SEND_VAL                                                 '...Unserializing+cart...'
          3        DO_ICALL                                                 
   78     4        INIT_FCALL                                               'json_decode'
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 <true>
          7        DO_ICALL                                         $4      
          8        ASSIGN                                                   !1, $4
   80     9        FETCH_OBJ_R                                      ~6      'reflector'
         10        INIT_METHOD_CALL                                         ~6, 'newLazyGhost'
         11        DECLARE_LAMBDA_FUNCTION                          ~7      [0]
         12        BIND_LEXICAL                                             ~7, !1
   84    13        SEND_VAL_EX                                              ~7
   80    14        DO_FCALL                                      0  $8      
         15        ASSIGN                                                   !2, $8
   86    16        FETCH_OBJ_R                                      ~10     'reflector'
         17        INIT_METHOD_CALL                                         ~10, 'getProperty'
         18        SEND_VAL_EX                                              'id'
         19        DO_FCALL                                      0  $11     
         20        INIT_METHOD_CALL                                         $11, 'setRawValueWithoutLazyInitialization'
         21        SEND_VAR_EX                                              !2
         22        CHECK_FUNC_ARG                                           
         23        FETCH_DIM_FUNC_ARG                               $12     !1, 'id'
         24        SEND_FUNC_ARG                                            $12
         25        DO_FCALL                                      0          
   87    26        FETCH_OBJ_R                                      ~14     'reflector'
         27        INIT_METHOD_CALL                                         ~14, 'getProperty'
         28        SEND_VAL_EX                                              'total'
         29        DO_FCALL                                      0  $15     
         30        INIT_METHOD_CALL                                         $15, 'setRawValueWithoutLazyInitialization'
         31        SEND_VAR_EX                                              !2
         32        CHECK_FUNC_ARG                                           
         33        FETCH_DIM_FUNC_ARG                               $16     !1, 'total'
         34        SEND_FUNC_ARG                                            $16
         35        DO_FCALL                                      0          
   88    36        FETCH_OBJ_R                                      ~18     'reflector'
         37        INIT_METHOD_CALL                                         ~18, 'getProperty'
         38        SEND_VAL_EX                                              'quantity'
         39        DO_FCALL                                      0  $19     
         40        INIT_METHOD_CALL                                         $19, 'setRawValueWithoutLazyInitialization'
         41        SEND_VAR_EX                                              !2
         42        CHECK_FUNC_ARG                                           
         43        FETCH_DIM_FUNC_ARG                               $20     !1, 'quantity'
         44        SEND_FUNC_ARG                                            $20
         45        DO_FCALL                                      0          
   90    46        VERIFY_RETURN_TYPE                                       !2
         47      > RETURN                                                   !2
   91    48*       VERIFY_RETURN_TYPE                                       
         49*     > 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/1uR3Y
function name:  {closure}
number of ops:  23
compiled vars:  !0 = $newCart, !1 = $previousCart, !2 = $products
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   81     2        INIT_FCALL                                               'var_dump'
          3        SEND_VAL                                                 '...Loading+products...'
          4        DO_ICALL                                                 
   82     5        FETCH_THIS                                       $4      
          6        FETCH_OBJ_R                                      ~5      $4, 'productRepository'
          7        INIT_METHOD_CALL                                         ~5, 'findByIds'
          8        CHECK_FUNC_ARG                                           
          9        FETCH_DIM_FUNC_ARG                               $6      !1, 'products'
         10        SEND_FUNC_ARG                                            $6
         11        DO_FCALL                                      0  $7      
         12        ASSIGN                                                   !2, $7
   83    13        FETCH_THIS                                       $9      
         14        FETCH_OBJ_R                                      ~10     $9, 'reflector'
         15        INIT_METHOD_CALL                                         ~10, 'getProperty'
         16        SEND_VAL_EX                                              'products'
         17        DO_FCALL                                      0  $11     
         18        INIT_METHOD_CALL                                         $11, 'setValue'
         19        SEND_VAR_EX                                              !0
         20        SEND_VAR_EX                                              !2
         21        DO_FCALL                                      0          
   84    22      > RETURN                                                   null

End of Dynamic Function 0

End of function unserialize

End of class CartRepository.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
192.57 ms | 1034 KiB | 17 Q