DPC2021: PHP Enums - Ayesh Karunaratne

Php

PHP Enums: Discover the new type in PHP 8.1, introducing a more restrictive and modern way to define constants and improve code maintainability and readability.

Key takeaways
  • Enums are a type in PHP 8.1, similar to classes, but with limitations.
  • Enums cannot have properties, they are readonly and cannot be extended.
  • Enums implement an interface called UnitEnum.
  • Enums have a fixed number of members and their values are contained within the class.
  • Enums can be used as property types or as return types for methods.
  • Enums have a unique syntax in PHP, using the enum keyword.
  • Enums support static methods, but not static properties.
  • Enums are not meant to be used with floats, boolean values, or class constants.
  • Enums are used with unit enums and backed enums, with unit enums being the default.
  • Enum members are readonly and cannot be added or removed.
  • Enums are a type, but they are not a class, and they do not support inheritance.
  • Enums are used to define a fixed set of constants.
  • Enums can be used in conjunction with type hints in method declarations.
  • Enums can be used with JSON data and APIs.
  • Enums can be used to create more readable and maintainable code.
  • Enums are meant to be used with a fixed set of values.
  • Enums are a more restrictive version of classes, with limitations.
  • Enums are not meant to be used with dynamic properties or methods.
  • Enums can be used to define type-safe constants.
  • Enums can be used with IDEs and autocompletion.
  • Enums have a syntax similar to classes, but with some differences.
  • Enums are a more modern way of defining constants.
  • Enums can be used in conjunction with type-safe code.
  • Enums are meant to be used with a fixed set of states or conditions.
  • Enums can be used to create more robust and maintainable code.
  • Enums can be used with discrete values.