Enum in PHP 8.1 - what is enum for and how is it implemented in PHP

A few days later, the vote ends on the first iteration of the enum implementation in PHP 8.1 . It is already clear that there are much more votes β€œfor”, so let's go through briefly and see what the authors of the language have prepared for us.







Why do we need enum?



Why do we need enum at all? In essence, they serve the purpose of improved type declarations. Let's look at an example without and with enums. Let's say we sell cars in three colors: red, black and white. How to describe the color, which type to choose?







class Car {
    private string $color;

    function setColor(string $color): void {
        $this->color = $color;
    }
}
      
      





If we describe the color of the car as a simple string, then, firstly, when calling $ myCar-> setColor (..), it is not clear what kind of string to write there. β€œRed” or β€œRED” or β€œ# ff0000”, and secondly, it's easy to make a mistake by accidentally slipping something wrong (an empty string, for example). The same will happen if you use numbers instead of strings, for example.







This leads to the fact that many php programmers create constants and combine them in one class to clearly see all the options.







class Color {
    public const RED   = "red";
    public const BLACK = "black";
    public const WHITE = "white";
}
      
      





and setting the color, write $myCar->setColor(Color::RED);









. $myCar->setColor(...), , - . - .







, . enum







Pure enum



(pure enum), enum :







enum Color {
    case Red;
    case Black;
    case White;
}
      
      





, :







class Car {
    private Color $color;

    function setColor(Color $color): void {
        $this->color = $color;
    }
}
      
      





, . setColor : $myCar->setColor(Color::White), . . .







case- (Color::Red, Color::Black, Color::White) Color ( instanseof ). .. 0,1,2 , . >, . $name:







print Color::Red->name; //   β€œRed”
      
      





Enum



, . enum , string. . , .







enum Color: string {
    case Red = "R";
    case Black = "B";
    case White = "W";
}
      
      





:







Color::Red->value  //  β€œR”
      
      





, .. case , :







Color::from("R") //   Color::Red
      
      





"case" enum . . , .







RFC.







interface Colorful {
  public function color(): string;
}

trait Rectangle {
  public function shape(): string {
    return "Rectangle";
  }
}

enum Suit implements Colorful {
  use Rectangle;

  case Hearts;
  case Diamonds;
  case Clubs;
  case Spades;

  public function color(): string {
    return match($this) {
      Suit::Hearts, Suit::Diamonds => 'Red',
      Suit::Clubs, Suit::Spaces => 'Black',
    };
  }
}
      
      





$this case, .







, match. , match .







enum PHP, , , - , enum (, Go).







β€” . Tagged Unions (-)



There are RFCs that take the idea of ​​enums further so that you can store values ​​of different types in one enum. Like Rust, for example. Then it will be possible to make, say, enum Result with two cases Result :: Ok and Result :: Err, and these objects will store data: Ok will store the result, and Err will store an error, each of these values ​​will have its own type.







And all this is not in Rust or Haskell, but in PHP!







We will tell you about this in detail in the next posts of the Cross Join telegram channel , do not forget to subscribe.








All Articles