3v4l.org

run code in 500+ PHP versions simultaneously
<?php readonly class ValidAge { private const MINIMUM_AGE = 10; private function __construct(public int $age) {} public static function createFromInt(int $age): self|NotValidAge { if (self::MINIMUM_AGE >= $age) { return new NotValidAge($age, 'Too old'); } return new self($age); } } readonly class NotValidAge { public function __construct( public int $age, public string $error ) {} } class ValidateAge { public function __invoke(int $age): ValidAge|NotValidAge { return ValidAge::createFromInt($age); } } class User { public function __construct( public int $id, public ValidAge $age ) {} } $rawAge = 33; $age = (new ValidateAge)($rawAge); $user = new User(1, $age); var_dump($user); $wrongAge = 8; $notValidAge = (new ValidateAge)($wrongAge); $anotherUser = new User(1, $notValidAge); var_dump($anotherUser);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WksJS
function name:  (null)
number of ops:  31
compiled vars:  !0 = $rawAge, !1 = $age, !2 = $user, !3 = $wrongAge, !4 = $notValidAge, !5 = $anotherUser
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   38     0  E >   ASSIGN                                                       !0, 33
   39     1        NEW                                                  $7      'ValidateAge'
          2        DO_FCALL                                          0          
          3        INIT_DYNAMIC_CALL                                            $7
          4        SEND_VAR_EX                                                  !0
          5        DO_FCALL                                          0  $9      
          6        ASSIGN                                                       !1, $9
   40     7        NEW                                                  $11     'User'
          8        SEND_VAL_EX                                                  1
          9        SEND_VAR_EX                                                  !1
         10        DO_FCALL                                          0          
         11        ASSIGN                                                       !2, $11
   42    12        INIT_FCALL                                                   'var_dump'
         13        SEND_VAR                                                     !2
         14        DO_ICALL                                                     
   44    15        ASSIGN                                                       !3, 8
   45    16        NEW                                                  $16     'ValidateAge'
         17        DO_FCALL                                          0          
         18        INIT_DYNAMIC_CALL                                            $16
         19        SEND_VAR_EX                                                  !3
         20        DO_FCALL                                          0  $18     
         21        ASSIGN                                                       !4, $18
   46    22        NEW                                                  $20     'User'
         23        SEND_VAL_EX                                                  1
         24        SEND_VAR_EX                                                  !4
         25        DO_FCALL                                          0          
         26        ASSIGN                                                       !5, $20
   48    27        INIT_FCALL                                                   'var_dump'
         28        SEND_VAR                                                     !5
         29        DO_ICALL                                                     
         30      > RETURN                                                       1

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

End of function __construct

Function createfromint:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WksJS
function name:  createFromInt
number of ops:  16
compiled vars:  !0 = $age
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    7     0  E >   RECV                                                 !0      
    8     1        IS_SMALLER_OR_EQUAL                                          !0, 10
          2      > JMPZ                                                         ~1, ->9
    9     3    >   NEW                                                  $2      'NotValidAge'
          4        SEND_VAR_EX                                                  !0
          5        SEND_VAL_EX                                                  'Too+old'
          6        DO_FCALL                                          0          
          7        VERIFY_RETURN_TYPE                                           $2
          8      > RETURN                                                       $2
   12     9    >   NEW                              self                $4      
         10        SEND_VAR_EX                                                  !0
         11        DO_FCALL                                          0          
         12        VERIFY_RETURN_TYPE                                           $4
         13      > RETURN                                                       $4
   13    14*       VERIFY_RETURN_TYPE                                           
         15*     > RETURN                                                       null

End of function createfromint

End of class ValidAge.

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

End of function __construct

End of class NotValidAge.

Class ValidateAge:
Function __invoke:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WksJS
function name:  __invoke
number of ops:  8
compiled vars:  !0 = $age
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   26     0  E >   RECV                                                 !0      
   27     1        INIT_STATIC_METHOD_CALL                                      'ValidAge', 'createFromInt'
          2        SEND_VAR                                                     !0
          3        DO_FCALL                                          0  $1      
          4        VERIFY_RETURN_TYPE                                           $1
          5      > RETURN                                                       $1
   28     6*       VERIFY_RETURN_TYPE                                           
          7*     > RETURN                                                       null

End of function __invoke

End of class ValidateAge.

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

End of function __construct

End of class User.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
154.93 ms | 1377 KiB | 14 Q