3v4l.org

run code in 500+ PHP versions simultaneously
<?php interface DomainEventInterface { } trait EventRecordingTrait { /** * @var array<DomainEventInterface> */ protected array $domainEvents = []; protected function recordThat(DomainEventInterface $domainEvent): void { $this->domainEvents[] = $domainEvent; } /** * @return array<DomainEventInterface> */ public function releaseEvents(): array { $domainEvents = $this->domainEvents; $this->domainEvents = []; return $domainEvents; } } readonly class Created implements DomainEventInterface { public function __construct( public string $name, public string $email ) { } } readonly class NameUpdated implements DomainEventInterface { public function __construct(public string $name) { } } class User { use EventRecordingTrait; // No intention of throwing events, it's just a simple object hydration! public function __construct( private string $name, private string $email, ) { } // intent of creating an instance and recording that specific event public static function create(string $name, string $email): self { $self = new User($name, $email); $self->recordThat(new Created($name, $email)); return $self; } // Wouldn't be necessary with some kind of asymetric visibility, but it's not a big hassle public function getName(): string { return $this->name; } // Here I would update the same $name attribute but throwing a different event public function updateName(string $name): void { if ($this->name === $name) { return; } $this->recordThat(new NameUpdated($name)); $this->name = $name; } // Wouldn't be necessary with some kind of asymetric visibility, but it's not a big hassle public function getEmail(): string { return $this->email; } } $user1 = new User('Hydrated from database', 'user1@example.com'); var_dump($user1); $user1->updateName('Mutation intent'); var_dump($user1); $user2 = User::create('Created from controller', 'user2@example.com'); var_dump($user2); $user2->updateName('Another mutation intent'); var_dump($user2);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZCVpG
function name:  (null)
number of ops:  32
compiled vars:  !0 = $user1, !1 = $user2
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   32     0  E >   DECLARE_CLASS                                                'created'
   41     1        DECLARE_CLASS                                                'nameupdated'
   48     2        DECLARE_CLASS                                                'user'
   93     3        NEW                                                  $2      'User'
          4        SEND_VAL_EX                                                  'Hydrated+from+database'
          5        SEND_VAL_EX                                                  'user1%40example.com'
          6        DO_FCALL                                          0          
          7        ASSIGN                                                       !0, $2
   94     8        INIT_FCALL                                                   'var_dump'
          9        SEND_VAR                                                     !0
         10        DO_ICALL                                                     
   95    11        INIT_METHOD_CALL                                             !0, 'updateName'
         12        SEND_VAL_EX                                                  'Mutation+intent'
         13        DO_FCALL                                          0          
   96    14        INIT_FCALL                                                   'var_dump'
         15        SEND_VAR                                                     !0
         16        DO_ICALL                                                     
   99    17        INIT_STATIC_METHOD_CALL                                      'User', 'create'
         18        SEND_VAL_EX                                                  'Created+from+controller'
         19        SEND_VAL_EX                                                  'user2%40example.com'
         20        DO_FCALL                                          0  $8      
         21        ASSIGN                                                       !1, $8
  100    22        INIT_FCALL                                                   'var_dump'
         23        SEND_VAR                                                     !1
         24        DO_ICALL                                                     
  101    25        INIT_METHOD_CALL                                             !1, 'updateName'
         26        SEND_VAL_EX                                                  'Another+mutation+intent'
         27        DO_FCALL                                          0          
  102    28        INIT_FCALL                                                   'var_dump'
         29        SEND_VAR                                                     !1
         30        DO_ICALL                                                     
         31      > RETURN                                                       1

Class DomainEventInterface: [no user functions]
Class EventRecordingTrait:
Function recordthat:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZCVpG
function name:  recordThat
number of ops:  5
compiled vars:  !0 = $domainEvent
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   14     0  E >   RECV                                                 !0      
   16     1        FETCH_OBJ_W                                          $1      'domainEvents'
          2        ASSIGN_DIM                                                   $1
          3        OP_DATA                                                      !0
   17     4      > RETURN                                                       null

End of function recordthat

Function releaseevents:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZCVpG
function name:  releaseEvents
number of ops:  8
compiled vars:  !0 = $domainEvents
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   24     0  E >   FETCH_OBJ_R                                          ~1      'domainEvents'
          1        ASSIGN                                                       !0, ~1
   25     2        ASSIGN_OBJ                                                   'domainEvents'
          3        OP_DATA                                                      <array>
   27     4        VERIFY_RETURN_TYPE                                           !0
          5      > RETURN                                                       !0
   28     6*       VERIFY_RETURN_TYPE                                           
          7*     > RETURN                                                       null

End of function releaseevents

End of class EventRecordingTrait.

Class Created:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZCVpG
function name:  __construct
number of ops:  7
compiled vars:  !0 = $name, !1 = $email
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   35     0  E >   RECV                                                 !0      
   36     1        RECV                                                 !1      
   35     2        ASSIGN_OBJ                                                   'name'
          3        OP_DATA                                                      !0
   36     4        ASSIGN_OBJ                                                   'email'
          5        OP_DATA                                                      !1
   38     6      > RETURN                                                       null

End of function __construct

End of class Created.

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

End of function __construct

End of class NameUpdated.

Class User:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZCVpG
function name:  __construct
number of ops:  7
compiled vars:  !0 = $name, !1 = $email
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   54     0  E >   RECV                                                 !0      
   55     1        RECV                                                 !1      
   54     2        ASSIGN_OBJ                                                   'name'
          3        OP_DATA                                                      !0
   55     4        ASSIGN_OBJ                                                   'email'
          5        OP_DATA                                                      !1
   57     6      > RETURN                                                       null

End of function __construct

Function create:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZCVpG
function name:  create
number of ops:  18
compiled vars:  !0 = $name, !1 = $email, !2 = $self
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   60     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   62     2        NEW                                                  $3      'User'
          3        SEND_VAR_EX                                                  !0
          4        SEND_VAR_EX                                                  !1
          5        DO_FCALL                                          0          
          6        ASSIGN                                                       !2, $3
   64     7        INIT_METHOD_CALL                                             !2, 'recordThat'
          8        NEW                                                  $6      'Created'
          9        SEND_VAR_EX                                                  !0
         10        SEND_VAR_EX                                                  !1
         11        DO_FCALL                                          0          
         12        SEND_VAR_NO_REF_EX                                           $6
         13        DO_FCALL                                          0          
   66    14        VERIFY_RETURN_TYPE                                           !2
         15      > RETURN                                                       !2
   67    16*       VERIFY_RETURN_TYPE                                           
         17*     > RETURN                                                       null

End of function create

Function getname:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZCVpG
function name:  getName
number of ops:  5
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   72     0  E >   FETCH_OBJ_R                                          ~0      'name'
          1        VERIFY_RETURN_TYPE                                           ~0
          2      > RETURN                                                       ~0
   73     3*       VERIFY_RETURN_TYPE                                           
          4*     > RETURN                                                       null

End of function getname

Function updatename:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZCVpG
function name:  updateName
number of ops:  14
compiled vars:  !0 = $name
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   76     0  E >   RECV                                                 !0      
   78     1        FETCH_OBJ_R                                          ~1      'name'
          2        IS_IDENTICAL                                                 !0, ~1
          3      > JMPZ                                                         ~2, ->5
   79     4    > > RETURN                                                       null
   82     5    >   INIT_METHOD_CALL                                             'recordThat'
          6        NEW                                                  $3      'NameUpdated'
          7        SEND_VAR_EX                                                  !0
          8        DO_FCALL                                          0          
          9        SEND_VAR_NO_REF_EX                                           $3
         10        DO_FCALL                                          0          
   83    11        ASSIGN_OBJ                                                   'name'
         12        OP_DATA                                                      !0
   84    13      > RETURN                                                       null

End of function updatename

Function getemail:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZCVpG
function name:  getEmail
number of ops:  5
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   89     0  E >   FETCH_OBJ_R                                          ~0      'email'
          1        VERIFY_RETURN_TYPE                                           ~0
          2      > RETURN                                                       ~0
   90     3*       VERIFY_RETURN_TYPE                                           
          4*     > RETURN                                                       null

End of function getemail

End of class User.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
156.4 ms | 1459 KiB | 14 Q