3v4l.org

run code in 300+ PHP versions simultaneously
<?php final readonly class Post { public int $id; public string $name; public string $email; public function greet(): string { return "Hello 👋"; } } final readonly class PostRepository { private ReflectionClass $reflector; public function __construct( ) { $this->reflector = new ReflectionClass(Post::class); } public function find(int $id): Post { var_dump("Loading post with ID $id"); $entity = $this->reflector->newLazyGhost(function ($entity) use ($id) { var_dump("Lazy initialization triggered for ID $id"); $data = $this->loadFromDatabase($id); $this->reflector->getProperty('name')->setValue($entity, $data['name']); $this->reflector->getProperty('email')->setValue($entity, $data['email']); }); // ID is already known and can be accessed without triggering initialization $this->reflector->getProperty('id')->setRawValueWithoutLazyInitialization($entity, $id); return $entity; } private function loadFromDatabase(int $id): array { // Simulate loading from database return [ 'name' => 'John Doe', 'email' => 'john.doe@example.com', ]; } } $em = new PostRepository(); $post = $em->find(42); var_dump($post); var_dump($post->greet()); // This will really trigger the lazy initialization $post->email; var_dump($post);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XNRiS
function name:  (null)
number of ops:  21
compiled vars:  !0 = $em, !1 = $post
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   NEW                                              $2      'PostRepository'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   53     3        INIT_METHOD_CALL                                         !0, 'find'
          4        SEND_VAL_EX                                              42
          5        DO_FCALL                                      0  $5      
          6        ASSIGN                                                   !1, $5
   54     7        INIT_FCALL                                               'var_dump'
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                                 
   55    10        INIT_FCALL                                               'var_dump'
         11        INIT_METHOD_CALL                                         !1, 'greet'
         12        DO_FCALL                                      0  $8      
         13        SEND_VAR                                                 $8
         14        DO_ICALL                                                 
   57    15        FETCH_OBJ_R                                      ~10     !1, 'email'
         16        FREE                                                     ~10
   58    17        INIT_FCALL                                               'var_dump'
         18        SEND_VAR                                                 !1
         19        DO_ICALL                                                 
         20      > RETURN                                                   1

Class Post:
Function greet:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XNRiS
function name:  greet
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E > > RETURN                                                   'Hello+%F0%9F%91%8B'
   12     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function greet

End of class Post.

Class PostRepository:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XNRiS
function name:  __construct
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   NEW                                              $1      'ReflectionClass'
          1        SEND_VAL_EX                                              'Post'
          2        DO_FCALL                                      0          
          3        ASSIGN_OBJ                                               'reflector'
          4        OP_DATA                                                  $1
   22     5      > RETURN                                                   null

End of function __construct

Function find:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XNRiS
function name:  find
number of ops:  25
compiled vars:  !0 = $id, !1 = $entity
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   26     1        INIT_FCALL                                               'var_dump'
          2        NOP                                                      
          3        FAST_CONCAT                                      ~2      'Loading+post+with+ID+', !0
          4        SEND_VAL                                                 ~2
          5        DO_ICALL                                                 
   28     6        FETCH_OBJ_R                                      ~4      'reflector'
          7        INIT_METHOD_CALL                                         ~4, 'newLazyGhost'
          8        DECLARE_LAMBDA_FUNCTION                          ~5      [0]
          9        BIND_LEXICAL                                             ~5, !0
   34    10        SEND_VAL_EX                                              ~5
   28    11        DO_FCALL                                      0  $6      
         12        ASSIGN                                                   !1, $6
   37    13        FETCH_OBJ_R                                      ~8      'reflector'
         14        INIT_METHOD_CALL                                         ~8, 'getProperty'
         15        SEND_VAL_EX                                              'id'
         16        DO_FCALL                                      0  $9      
         17        INIT_METHOD_CALL                                         $9, 'setRawValueWithoutLazyInitialization'
         18        SEND_VAR_EX                                              !1
         19        SEND_VAR_EX                                              !0
         20        DO_FCALL                                      0          
   39    21        VERIFY_RETURN_TYPE                                       !1
         22      > RETURN                                                   !1
   40    23*       VERIFY_RETURN_TYPE                                       
         24*     > 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/XNRiS
function name:  {closure}
number of ops:  35
compiled vars:  !0 = $entity, !1 = $id, !2 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   29     2        INIT_FCALL                                               'var_dump'
          3        NOP                                                      
          4        FAST_CONCAT                                      ~3      'Lazy+initialization+triggered+for+ID+', !1
          5        SEND_VAL                                                 ~3
          6        DO_ICALL                                                 
   31     7        FETCH_THIS                                       $5      
          8        INIT_METHOD_CALL                                         $5, 'loadFromDatabase'
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0  $6      
         11        ASSIGN                                                   !2, $6
   32    12        FETCH_THIS                                       $8      
         13        FETCH_OBJ_R                                      ~9      $8, 'reflector'
         14        INIT_METHOD_CALL                                         ~9, 'getProperty'
         15        SEND_VAL_EX                                              'name'
         16        DO_FCALL                                      0  $10     
         17        INIT_METHOD_CALL                                         $10, 'setValue'
         18        SEND_VAR_EX                                              !0
         19        CHECK_FUNC_ARG                                           
         20        FETCH_DIM_FUNC_ARG                               $11     !2, 'name'
         21        SEND_FUNC_ARG                                            $11
         22        DO_FCALL                                      0          
   33    23        FETCH_THIS                                       $13     
         24        FETCH_OBJ_R                                      ~14     $13, 'reflector'
         25        INIT_METHOD_CALL                                         ~14, 'getProperty'
         26        SEND_VAL_EX                                              'email'
         27        DO_FCALL                                      0  $15     
         28        INIT_METHOD_CALL                                         $15, 'setValue'
         29        SEND_VAR_EX                                              !0
         30        CHECK_FUNC_ARG                                           
         31        FETCH_DIM_FUNC_ARG                               $16     !2, 'email'
         32        SEND_FUNC_ARG                                            $16
         33        DO_FCALL                                      0          
   34    34      > RETURN                                                   null

End of Dynamic Function 0

End of function find

Function loadfromdatabase:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XNRiS
function name:  loadFromDatabase
number of ops:  4
compiled vars:  !0 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
   46     1      > RETURN                                                   <array>
   49     2*       VERIFY_RETURN_TYPE                                       
          3*     > RETURN                                                   null

End of function loadfromdatabase

End of class PostRepository.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.22 ms | 1014 KiB | 14 Q