3v4l.org

run code in 300+ PHP versions simultaneously
<?php enum Gender { case UNKNOWN; case MALE; case FEMALE; } /** * @template T as BackedEnum|UnitEnum * @param class-string<T> $enumClass * @param string $name * @return T|null */ function tryFromName(string $enumClass, string $name) { $constName = $enumClass . '::' . $name; return is_subclass_of($enumClass, UnitEnum::class) && defined($constName) ? constant($constName) : null; } var_dump(tryFromName(Gender::class, 'UNKNOWN'));// enum(Gender::UNKNOWN) var_dump(tryFromName(Gender::class, 'MALE'));// enum(Gender::MALE) var_dump(tryFromName(Gender::class, 'FEMALE'));// enum(Gender::FEMALE) var_dump(tryFromName(Gender::class, 'null'));// NULL
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8R2fP
function name:  (null)
number of ops:  30
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                            'gender'
   25     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'tryfromname'
          3        SEND_VAL                                                 'Gender'
          4        SEND_VAL                                                 'UNKNOWN'
          5        DO_FCALL                                      0  $0      
          6        SEND_VAR                                                 $0
          7        DO_ICALL                                                 
   26     8        INIT_FCALL                                               'var_dump'
          9        INIT_FCALL                                               'tryfromname'
         10        SEND_VAL                                                 'Gender'
         11        SEND_VAL                                                 'MALE'
         12        DO_FCALL                                      0  $2      
         13        SEND_VAR                                                 $2
         14        DO_ICALL                                                 
   27    15        INIT_FCALL                                               'var_dump'
         16        INIT_FCALL                                               'tryfromname'
         17        SEND_VAL                                                 'Gender'
         18        SEND_VAL                                                 'FEMALE'
         19        DO_FCALL                                      0  $4      
         20        SEND_VAR                                                 $4
         21        DO_ICALL                                                 
   28    22        INIT_FCALL                                               'var_dump'
         23        INIT_FCALL                                               'tryfromname'
         24        SEND_VAL                                                 'Gender'
         25        SEND_VAL                                                 'null'
         26        DO_FCALL                                      0  $6      
         27        SEND_VAR                                                 $6
         28        DO_ICALL                                                 
         29      > RETURN                                                   1

Function tryfromname:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 20
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/8R2fP
function name:  tryFromName
number of ops:  23
compiled vars:  !0 = $enumClass, !1 = $name, !2 = $constName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   18     2        CONCAT                                           ~3      !0, '%3A%3A'
          3        CONCAT                                           ~4      ~3, !1
          4        ASSIGN                                                   !2, ~4
   20     5        INIT_FCALL                                               'is_subclass_of'
          6        SEND_VAR                                                 !0
          7        SEND_VAL                                                 'UnitEnum'
          8        DO_ICALL                                         $6      
          9      > JMPZ_EX                                          ~7      $6, ->14
         10    >   INIT_FCALL                                               'defined'
         11        SEND_VAR                                                 !2
         12        DO_ICALL                                         $8      
         13        BOOL                                             ~7      $8
         14    > > JMPZ                                                     ~7, ->20
   21    15    >   INIT_FCALL                                               'constant'
         16        SEND_VAR                                                 !2
         17        DO_ICALL                                         $9      
         18        QM_ASSIGN                                        ~10     $9
         19      > JMP                                                      ->21
   22    20    >   QM_ASSIGN                                        ~10     null
         21    > > RETURN                                                   ~10
   23    22*     > RETURN                                                   null

End of function tryfromname

Class Gender: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.79 ms | 1010 KiB | 21 Q