3v4l.org

run code in 500+ PHP versions simultaneously
<?php #[Attribute(Attribute::TARGET_CLASS_CONSTANT)] final class SuitInfo { public function __construct(public readonly string $symbol, public readonly string $color) { } } trait EnumDetailsTrait { final protected function getAttribute(string $className): mixed { $reflection = new ReflectionClassConstant($this::class, $this->name); $classAttributes = $reflection->getAttributes($className); return match (count($classAttributes)) { // Return a blank details 0 => new $className(), // Return the provided information 1 => reset($classAttributes)->newInstance(), // PHP doesn't enforce not-repeatable for attributes, so we have to default => throw new RuntimeException(sprintf('%1$s cases must have a single attribute of %2$s ', self::class, $className)), }; } } enum Suit: string { use EnumDetailsTrait; #[SuitInfo('♣', 'black')] case Clubs = 'clubs'; #[SuitInfo('♦', 'red')] case Diamonds = 'diamonds'; #[SuitInfo('♥', 'red')] case Hearts = 'hearts'; #[SuitInfo('♠', 'black')] case Spades = 'spades'; public function getSymbol(): string { return $this->getAttribute(SuitInfo::class)->symbol; } public function getColor() : string { return $this->getAttribute(SuitInfo::class)->color; } } class Card { public function __construct(public readonly string $value, public readonly Suit $suit){} } $myCard = new Card('Q', Suit::Clubs); echo $myCard->suit->getSymbol(); echo PHP_EOL; echo $myCard->suit->getColor();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QKBvU
function name:  (null)
number of ops:  17
compiled vars:  !0 = $myCard
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   33     0  E >   DECLARE_CLASS                                                'suit'
   64     1        NEW                                                  $1      'Card'
          2        SEND_VAL_EX                                                  'Q'
          3        FETCH_CLASS_CONSTANT                                 ~2      'Suit', 'Clubs'
          4        SEND_VAL_EX                                                  ~2
          5        DO_FCALL                                          0          
          6        ASSIGN                                                       !0, $1
   66     7        FETCH_OBJ_R                                          ~5      !0, 'suit'
          8        INIT_METHOD_CALL                                             ~5, 'getSymbol'
          9        DO_FCALL                                          0  $6      
         10        ECHO                                                         $6
   67    11        ECHO                                                         '%0A'
   68    12        FETCH_OBJ_R                                          ~7      !0, 'suit'
         13        INIT_METHOD_CALL                                             ~7, 'getColor'
         14        DO_FCALL                                          0  $8      
         15        ECHO                                                         $8
         16      > RETURN                                                       1

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

End of function __construct

End of class SuitInfo.

Class EnumDetailsTrait:
Function getattribute:
Finding entry points
Branch analysis from position: 0
3 jumps found. (Code = 195) Position 1 = 16, Position 2 = 21, Position 3 = 28
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
Branch analysis from position: 28
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/QKBvU
function name:  getAttribute
number of ops:  44
compiled vars:  !0 = $className, !1 = $reflection, !2 = $classAttributes
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   13     0  E >   RECV                                                 !0      
   15     1        NEW                                                  $3      'ReflectionClassConstant'
          2        FETCH_THIS                                           ~4      
          3        FETCH_CLASS_NAME                                     ~5      ~4
          4        SEND_VAL_EX                                                  ~5
          5        CHECK_FUNC_ARG                                               
          6        FETCH_OBJ_FUNC_ARG                                   $6      'name'
          7        SEND_FUNC_ARG                                                $6
          8        DO_FCALL                                          0          
          9        ASSIGN                                                       !1, $3
   17    10        INIT_METHOD_CALL                                             !1, 'getAttributes'
         11        SEND_VAR_EX                                                  !0
         12        DO_FCALL                                          0  $9      
         13        ASSIGN                                                       !2, $9
   19    14        COUNT                                                ~11     !2
         15      > MATCH                                                        ~11, [ 0:->16, 1:->21, ], ->28
   21    16    >   FETCH_CLASS                                       0  $13     !0
         17        NEW                                                  $14     $13
         18        DO_FCALL                                          0          
         19        QM_ASSIGN                                            ~16     $14
         20      > JMP                                                          ->40
   24    21    >   INIT_FCALL                                                   'reset'
         22        SEND_REF                                                     !2
         23        DO_ICALL                                             $17     
         24        INIT_METHOD_CALL                                             $17, 'newInstance'
         25        DO_FCALL                                          0  $18     
         26        QM_ASSIGN                                            ~16     $18
         27      > JMP                                                          ->40
   27    28    >   NEW                                                  $19     'RuntimeException'
         29        INIT_FCALL                                                   'sprintf'
         30        SEND_VAL                                                     '%251%24s+cases+must+have+a+single+attribute+of+%252%24s+'
         31        FETCH_CLASS_NAME                                     ~20     
         32        SEND_VAL                                                     ~20
         33        SEND_VAR                                                     !0
         34        DO_ICALL                                             $21     
         35        SEND_VAR_NO_REF_EX                                           $21
         36        DO_FCALL                                          0          
         37      > THROW                                             1          $19
         38*       QM_ASSIGN                                            ~16     <true>
         39*       JMP                                                          ->40
         40    >   FREE                                                         ~11
         41      > RETURN                                                       ~16
   29    42*       VERIFY_RETURN_TYPE                                           
         43*     > RETURN                                                       null

End of function getattribute

End of class EnumDetailsTrait.

Class Suit:
Function getsymbol:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QKBvU
function name:  getSymbol
number of ops:  8
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   51     0  E >   INIT_METHOD_CALL                                             'getAttribute'
          1        SEND_VAL_EX                                                  'SuitInfo'
          2        DO_FCALL                                          0  $0      
          3        FETCH_OBJ_R                                          ~1      $0, 'symbol'
          4        VERIFY_RETURN_TYPE                                           ~1
          5      > RETURN                                                       ~1
   52     6*       VERIFY_RETURN_TYPE                                           
          7*     > RETURN                                                       null

End of function getsymbol

Function getcolor:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QKBvU
function name:  getColor
number of ops:  8
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   56     0  E >   INIT_METHOD_CALL                                             'getAttribute'
          1        SEND_VAL_EX                                                  'SuitInfo'
          2        DO_FCALL                                          0  $0      
          3        FETCH_OBJ_R                                          ~1      $0, 'color'
          4        VERIFY_RETURN_TYPE                                           ~1
          5      > RETURN                                                       ~1
   57     6*       VERIFY_RETURN_TYPE                                           
          7*     > RETURN                                                       null

End of function getcolor

End of class Suit.

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

End of function __construct

End of class Card.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
157.3 ms | 1345 KiB | 15 Q