3v4l.org

run code in 500+ PHP versions simultaneously
<?php // declare(strict_types=1); trait changed_tryFrom { public static function changed_tryFrom(mixed $value): self|null { if ($value instanceof self) { // nothing to do, already a valid "self". return $value; } elseif (null === $value) { // A backed enum of type int or type string can never contain the null value. // Prevent warning "Passing null to parameter #1 ($value) of type string|int is deprecated". return null; } try { return self::from($value); } catch (\Throwable $ex) { return null; } } } enum SuitAsInt: int { use \changed_tryFrom; case Clubs = 1; case Diamonds = 2; case Hearts = 3; case Spades = 4; } enum SuitAsString: string { use \changed_tryFrom; case Clubs = '1'; case Diamonds = '2'; case Hearts = '3'; case Spades = '4'; } $testcases = [ new \DateTimeImmutable(), null, 4, 4.0, '4', \SuitAsInt::Spades, \SuitAsString::Spades, ]; foreach ($testcases as $testcase) { echo '----------------------------------------'."\n"; echo '--------------[ TESTCASE ]--------------'."\n"; echo '----------------------------------------'."\n"; var_dump($testcase); echo "\n"; echo '------- [ INT backed enum ] -------'."\n"; echo '--- SuitAsInt::tryFrom()'."\n"; try { $value = \SuitAsInt::tryFrom($testcase); if (null !== $value) { echo 'Value is '.$value->name.' with value '.$value->value.'.'."\n"; } else { echo 'Value is NULL.'."\n"; } } catch (\Throwable $exception) { echo $exception::class, ': ', $exception->getMessage(), "\n"; } echo '--- SuitAsInt::changed_tryFrom() [CHANGED]'."\n"; try { $value = \SuitAsInt::changed_tryFrom($testcase); if (null !== $value) { echo 'Value is '.$value->name.' with value '.$value->value.'.'."\n"; } else { echo 'Value is NULL.'."\n"; } } catch (\Throwable $exception) { echo $exception::class, ': ', $exception->getMessage(), "\n"; } echo "\n"; echo '------- [ STRING backed enum ] -------'."\n"; echo '--- SuitAsString::tryFrom()'."\n"; try { $value = \SuitAsString::tryFrom($testcase); if (null !== $value) { echo 'Value is '.$value->name.' with value '.$value->value.'.'."\n"; } else { echo 'Value is NULL.'."\n"; } } catch (\Throwable $exception) { echo $exception::class, ': ', $exception->getMessage(), "\n"; } echo '--- SuitAsString::changed_tryFrom() [CHANGED]'."\n"; try { $value = \SuitAsString::changed_tryFrom($testcase); if (null !== $value) { echo 'Value is '.$value->name.' with value '.$value->value.'.'."\n"; } else { echo 'Value is NULL.'."\n"; } } catch (\Throwable $exception) { echo $exception::class, ': ', $exception->getMessage(), "\n"; } echo "\n"; echo "\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 133
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 133
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 40
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 66
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
2 jumps found. (Code = 43) Position 1 = 85, Position 2 = 94
Branch analysis from position: 85
1 jumps found. (Code = 42) Position 1 = 95
Branch analysis from position: 95
1 jumps found. (Code = 42) Position 1 = 104
Branch analysis from position: 104
2 jumps found. (Code = 43) Position 1 = 111, Position 2 = 120
Branch analysis from position: 111
1 jumps found. (Code = 42) Position 1 = 121
Branch analysis from position: 121
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 120
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 94
1 jumps found. (Code = 42) Position 1 = 104
Branch analysis from position: 104
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
Branch analysis from position: 133
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 133
Found catch point at position: 42
Branch analysis from position: 42
2 jumps found. (Code = 107) Position 1 = 43, Position 2 = -2
Branch analysis from position: 43
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 66
Branch analysis from position: 57
Branch analysis from position: 66
Found catch point at position: 68
Branch analysis from position: 68
2 jumps found. (Code = 107) Position 1 = 69, Position 2 = -2
Branch analysis from position: 69
2 jumps found. (Code = 43) Position 1 = 85, Position 2 = 94
Branch analysis from position: 85
Branch analysis from position: 94
Found catch point at position: 96
Branch analysis from position: 96
2 jumps found. (Code = 107) Position 1 = 97, Position 2 = -2
Branch analysis from position: 97
2 jumps found. (Code = 43) Position 1 = 111, Position 2 = 120
Branch analysis from position: 111
Branch analysis from position: 120
Found catch point at position: 122
Branch analysis from position: 122
2 jumps found. (Code = 107) Position 1 = 123, Position 2 = -2
Branch analysis from position: 123
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
filename:       /in/sNm6E
function name:  (null)
number of ops:  135
compiled vars:  !0 = $testcases, !1 = $testcase, !2 = $value, !3 = $exception
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   26     0  E >   DECLARE_CLASS                                                'suitasint'
   36     1        DECLARE_CLASS                                                'suitasstring'
   47     2        NEW                                                  $4      'DateTimeImmutable'
          3        DO_FCALL                                          0          
          4        INIT_ARRAY                                           ~6      $4
          5        ADD_ARRAY_ELEMENT                                    ~6      null
   49     6        ADD_ARRAY_ELEMENT                                    ~6      4
   50     7        ADD_ARRAY_ELEMENT                                    ~6      4
   51     8        ADD_ARRAY_ELEMENT                                    ~6      '4'
   52     9        FETCH_CLASS_CONSTANT                                 ~7      'SuitAsInt', 'Spades'
         10        ADD_ARRAY_ELEMENT                                    ~6      ~7
   53    11        FETCH_CLASS_CONSTANT                                 ~8      'SuitAsString', 'Spades'
         12        ADD_ARRAY_ELEMENT                                    ~6      ~8
   46    13        ASSIGN                                                       !0, ~6
   56    14      > FE_RESET_R                                           $10     !0, ->133
         15    > > FE_FETCH_R                                                   $10, !1, ->133
   57    16    >   ECHO                                                         '----------------------------------------%0A'
   58    17        ECHO                                                         '--------------%5B+TESTCASE+%5D--------------%0A'
   59    18        ECHO                                                         '----------------------------------------%0A'
   60    19        INIT_FCALL                                                   'var_dump'
         20        SEND_VAR                                                     !1
         21        DO_ICALL                                                     
   62    22        ECHO                                                         '%0A'
   63    23        ECHO                                                         '-------+%5B+INT+backed+enum+%5D+-------%0A'
   65    24        ECHO                                                         '---+SuitAsInt%3A%3AtryFrom%28%29%0A'
   67    25        INIT_STATIC_METHOD_CALL                                      'SuitAsInt', 'tryFrom'
         26        SEND_VAR_EX                                                  !1
         27        DO_FCALL                                          0  $12     
         28        ASSIGN                                                       !2, $12
   68    29        TYPE_CHECK                                      1020          !2
         30      > JMPZ                                                         ~14, ->40
   69    31    >   FETCH_OBJ_R                                          ~15     !2, 'name'
         32        CONCAT                                               ~16     'Value+is+', ~15
         33        CONCAT                                               ~17     ~16, '+with+value+'
         34        FETCH_OBJ_R                                          ~18     !2, 'value'
         35        CONCAT                                               ~19     ~17, ~18
         36        CONCAT                                               ~20     ~19, '.'
         37        CONCAT                                               ~21     ~20, '%0A'
         38        ECHO                                                         ~21
   68    39      > JMP                                                          ->41
   71    40    >   ECHO                                                         'Value+is+NULL.%0A'
         41    > > JMP                                                          ->50
   73    42  E > > CATCH                                           last         'Throwable'
   74    43    >   FETCH_CLASS_NAME                                     ~22     !3
         44        ECHO                                                         ~22
         45        ECHO                                                         '%3A+'
         46        INIT_METHOD_CALL                                             !3, 'getMessage'
         47        DO_FCALL                                          0  $23     
         48        ECHO                                                         $23
         49        ECHO                                                         '%0A'
   77    50    >   ECHO                                                         '---+SuitAsInt%3A%3Achanged_tryFrom%28%29+%5BCHANGED%5D%0A'
   79    51        INIT_STATIC_METHOD_CALL                                      'SuitAsInt', 'changed_tryFrom'
         52        SEND_VAR_EX                                                  !1
         53        DO_FCALL                                          0  $24     
         54        ASSIGN                                                       !2, $24
   80    55        TYPE_CHECK                                      1020          !2
         56      > JMPZ                                                         ~26, ->66
   81    57    >   FETCH_OBJ_R                                          ~27     !2, 'name'
         58        CONCAT                                               ~28     'Value+is+', ~27
         59        CONCAT                                               ~29     ~28, '+with+value+'
         60        FETCH_OBJ_R                                          ~30     !2, 'value'
         61        CONCAT                                               ~31     ~29, ~30
         62        CONCAT                                               ~32     ~31, '.'
         63        CONCAT                                               ~33     ~32, '%0A'
         64        ECHO                                                         ~33
   80    65      > JMP                                                          ->67
   83    66    >   ECHO                                                         'Value+is+NULL.%0A'
         67    > > JMP                                                          ->76
   85    68  E > > CATCH                                           last         'Throwable'
   86    69    >   FETCH_CLASS_NAME                                     ~34     !3
         70        ECHO                                                         ~34
         71        ECHO                                                         '%3A+'
         72        INIT_METHOD_CALL                                             !3, 'getMessage'
         73        DO_FCALL                                          0  $35     
         74        ECHO                                                         $35
         75        ECHO                                                         '%0A'
   89    76    >   ECHO                                                         '%0A'
   90    77        ECHO                                                         '-------+%5B+STRING+backed+enum+%5D+-------%0A'
   92    78        ECHO                                                         '---+SuitAsString%3A%3AtryFrom%28%29%0A'
   94    79        INIT_STATIC_METHOD_CALL                                      'SuitAsString', 'tryFrom'
         80        SEND_VAR_EX                                                  !1
         81        DO_FCALL                                          0  $36     
         82        ASSIGN                                                       !2, $36
   95    83        TYPE_CHECK                                      1020          !2
         84      > JMPZ                                                         ~38, ->94
   96    85    >   FETCH_OBJ_R                                          ~39     !2, 'name'
         86        CONCAT                                               ~40     'Value+is+', ~39
         87        CONCAT                                               ~41     ~40, '+with+value+'
         88        FETCH_OBJ_R                                          ~42     !2, 'value'
         89        CONCAT                                               ~43     ~41, ~42
         90        CONCAT                                               ~44     ~43, '.'
         91        CONCAT                                               ~45     ~44, '%0A'
         92        ECHO                                                         ~45
   95    93      > JMP                                                          ->95
   98    94    >   ECHO                                                         'Value+is+NULL.%0A'
         95    > > JMP                                                          ->104
  100    96  E > > CATCH                                           last         'Throwable'
  101    97    >   FETCH_CLASS_NAME                                     ~46     !3
         98        ECHO                                                         ~46
         99        ECHO                                                         '%3A+'
        100        INIT_METHOD_CALL                                             !3, 'getMessage'
        101        DO_FCALL                                          0  $47     
        102        ECHO                                                         $47
        103        ECHO                                                         '%0A'
  104   104    >   ECHO                                                         '---+SuitAsString%3A%3Achanged_tryFrom%28%29+%5BCHANGED%5D%0A'
  106   105        INIT_STATIC_METHOD_CALL                                      'SuitAsString', 'changed_tryFrom'
        106        SEND_VAR_EX                                                  !1
        107        DO_FCALL                                          0  $48     
        108        ASSIGN                                                       !2, $48
  107   109        TYPE_CHECK                                      1020          !2
        110      > JMPZ                                                         ~50, ->120
  108   111    >   FETCH_OBJ_R                                          ~51     !2, 'name'
        112        CONCAT                                               ~52     'Value+is+', ~51
        113        CONCAT                                               ~53     ~52, '+with+value+'
        114        FETCH_OBJ_R                                          ~54     !2, 'value'
        115        CONCAT                                               ~55     ~53, ~54
        116        CONCAT                                               ~56     ~55, '.'
        117        CONCAT                                               ~57     ~56, '%0A'
        118        ECHO                                                         ~57
  107   119      > JMP                                                          ->121
  110   120    >   ECHO                                                         'Value+is+NULL.%0A'
        121    > > JMP                                                          ->130
  112   122  E > > CATCH                                           last         'Throwable'
  113   123    >   FETCH_CLASS_NAME                                     ~58     !3
        124        ECHO                                                         ~58
        125        ECHO                                                         '%3A+'
        126        INIT_METHOD_CALL                                             !3, 'getMessage'
        127        DO_FCALL                                          0  $59     
        128        ECHO                                                         $59
        129        ECHO                                                         '%0A'
  116   130    >   ECHO                                                         '%0A'
  117   131        ECHO                                                         '%0A'
   56   132      > JMP                                                          ->15
        133    >   FE_FREE                                                      $10
  118   134      > RETURN                                                       1

Class changed_tryFrom:
Function changed_tryfrom:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 15
Branch analysis from position: 15
2 jumps found. (Code = 107) Position 1 = 16, Position 2 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sNm6E
function name:  changed_tryFrom
number of ops:  19
compiled vars:  !0 = $value, !1 = $ex
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    7     0  E >   RECV                                                 !0      
    9     1        INSTANCEOF                                                   !0
          2      > JMPZ                                                         ~2, ->6
   11     3    >   VERIFY_RETURN_TYPE                                           !0
          4      > RETURN                                                       !0
    9     5*       JMP                                                          ->9
   12     6    >   TYPE_CHECK                                        2          !0
          7      > JMPZ                                                         ~3, ->9
   15     8    > > RETURN                                                       null
   19     9    >   INIT_STATIC_METHOD_CALL                                      'from'
         10        SEND_VAR_EX                                                  !0
         11        DO_FCALL                                          0  $4      
         12        VERIFY_RETURN_TYPE                                           $4
         13      > RETURN                                                       $4
         14*       JMP                                                          ->17
   20    15  E > > CATCH                                           last         'Throwable'
   21    16    > > RETURN                                                       null
   23    17*       VERIFY_RETURN_TYPE                                           
         18*     > RETURN                                                       null

End of function changed_tryfrom

End of class changed_tryFrom.

Class SuitAsInt: [no user functions]
Class SuitAsString: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
129.21 ms | 498 KiB | 9 Q