1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
|
<?xml version="1.0" encoding="utf-8"?>
<chapter xml:id="language.enumerations" xmlns="http://docbook.org/ns/docbook">
<title>Enumerations</title>
<sect1 xml:id="language.enumerations.overview">
<title>Enumerations overview</title>
<?phpdoc print-version-for="enumerations"?>
<para>
Enumerations, or "Enums", allow a developer to define a custom type that is limited to one
of a discrete number of possible values. That can be especially helpful when defining a
domain model, as it enables "making invalid states unrepresentable."
</para>
<para>
Enums appear in many languages with a variety of different features. In PHP,
Enums are a special kind of object. The Enum itself is a class, and its possible
cases are all single-instance objects of that class. That means Enum cases are
valid objects and may be used anywhere an object may be used, including type checks.
</para>
<para>
The most popular example of enumerations is the built-in boolean type, which is an
enumerated type with legal values &true; and &false;.
Enums allows developers to define their own arbitrarily robust enumerations.
</para>
</sect1>
<sect1 xml:id="language.enumerations.basics">
<title>Basic enumerations</title>
<para>
Enums are similar to classes, and share the same namespaces as classes, interfaces, and traits.
They are also autoloadable the same way. An Enum defines a new type, which has a fixed, limited
number of possible legal values.
</para>
<programlisting role="php">
<![CDATA[
<?php
enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
?>
]]>
</programlisting>
<para>
This declaration creates a new enumerated type named <literal>Suit</literal>, which has
four and only four legal values: <literal>Suit::Hearts</literal>, <literal>Suit::Diamonds</literal>,
<literal>Suit::Clubs</literal>, and <literal>Suit::Spades</literal>. Variables may be assigned
to one of those legal values. A function may be type checked against an enumerated type,
in which case only values of that type may be passed.
</para>
<programlisting role="php">
<![CDATA[
<?php
function pick_a_card(Suit $suit)
{
/* ... */
}
$val = Suit::Diamonds;
// OK
pick_a_card($val);
// OK
pick_a_card(Suit::Clubs);
// TypeError: pick_a_card(): Argument #1 ($suit) must be of type Suit, string given
pick_a_card('Spades');
?>
]]>
</programlisting>
<para>
An Enumeration may have zero or more <literal>case</literal> definitions, with no maximum.
A zero-case enum is syntactically valid, if rather useless.
</para>
<para>
For Enumeration cases, the same syntax rules apply as to any label in PHP, see
<link linkend="language.constants">Constants</link>.
</para>
<para>
By default, cases are not intrinsically backed by a scalar value. That is, <literal>Suit::Hearts</literal>
is not equal to <literal>"0"</literal>. Instead, each case is backed by a singleton object of that name. That means that:
</para>
<programlisting role="php">
<![CDATA[
<?php
$a = Suit::Spades;
$b = Suit::Spades;
$a === $b; // true
$a instanceof Suit; // true
?>
]]>
</programlisting>
<para>
It also means that enum values are never <literal><</literal> or <literal>></literal> each other,
since those comparisons are not meaningful on objects. Those comparisons will always return
&false; when working with enum values.
</para>
<para>
This type of case, with no related data, is called a "Pure Case." An Enum that contains
only Pure Cases is called a Pure Enum.
</para>
<para>
All Pure Cases are implemented as instances of their enum type. The enum type is represented internally as a class.
</para>
<para>
All Cases have a read-only property, <literal>name</literal>, that is the case-sensitive name
of the case itself.
</para>
<programlisting role="php">
<![CDATA[
<?php
print Suit::Spades->name;
// prints "Spades"
?>
]]>
</programlisting>
<para>
It is also possible to use the <function>defined</function> and <function>constant</function>
functions to check for the existence of or read an enum case if the name is obtained dynamically.
This is, however, discouraged as using <link linkend="language.enumerations.backed">Backed enums</link>
should work for most use cases.
</para>
</sect1>
<sect1 xml:id="language.enumerations.backed">
<title>Backed enumerations</title>
<para>
By default, Enumerated Cases have no scalar equivalent. They are simply singleton objects. However,
there are ample cases where an Enumerated Case needs to be able to round-trip to a database or
similar datastore, so having a built-in scalar (and thus trivially serializable) equivalent defined
intrinsically is useful.
</para>
<para>To define a scalar equivalent for an Enumeration, the syntax is as follows:</para>
<programlisting role="php">
<![CDATA[
<?php
enum Suit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}
?>
]]>
</programlisting>
<para>
A case that has a scalar equivalent is called a Backed Case, as it is "Backed"
by a simpler value. An Enum that contains all Backed Cases is called a "Backed Enum."
A Backed Enum may contain only Backed Cases. A Pure Enum may contain only Pure Cases.
</para>
<para>
A Backed Enum may be backed by types of <literal>int</literal> or <literal>string</literal>,
and a given enumeration supports only a single type at a time (that is, no union of <literal>int|string</literal>).
If an enumeration is marked as having a scalar equivalent, then all cases must have a unique
scalar equivalent defined explicitly. There are no auto-generated scalar equivalents
(e.g., sequential integers). Backed cases must be unique; two backed enum cases may
not have the same scalar equivalent. However, a constant may refer to a case, effectively
creating an alias. See <link linkend="language.enumerations.constants">Enumeration constants</link>.
</para>
<para>
Equivalent values must be literals or literal expressions. Constants and constant expressions
are not supported. That is, <literal>1 + 1</literal> is allowed, but <literal>1 + SOME_CONST</literal>
is not.
</para>
<para>
Backed Cases have an additional read-only property, <literal>value</literal>, which is the value
specified in the definition.
</para>
<programlisting role="php">
<![CDATA[
<?php
print Suit::Clubs->value;
// Prints "C"
?>
]]>
</programlisting>
<para>
In order to enforce the <literal>value</literal> property as read-only, a variable cannot
be assigned as a reference to it. That is, the following throws an error:
</para>
<programlisting role="php">
<![CDATA[
<?php
$suit = Suit::Clubs;
$ref = &$suit->value;
// Error: Cannot acquire reference to property Suit::$value
?>
]]>
</programlisting>
<para>
Backed enums implement an internal <interfacename>BackedEnum</interfacename> interface,
which exposes two additional methods:
</para>
<simplelist>
<member>
<literal>from(int|string): self</literal> will take a scalar and return the corresponding
Enum Case. If one is not found, it will throw a <classname>ValueError</classname>. This is mainly
useful in cases where the input scalar is trusted and a missing enum value should be
considered an application-stopping error.
</member>
<member>
<literal>tryFrom(int|string): ?self</literal> will take a scalar and return the
corresponding Enum Case. If one is not found, it will return <literal>null</literal>.
This is mainly useful in cases where the input scalar is untrusted and the caller wants
to implement their own error handling or default-value logic.
</member>
</simplelist>
<para>
The <literal>from()</literal> and <literal>tryFrom()</literal> methods follow standard
weak/strong typing rules. In weak typing mode, passing an integer or string is acceptable
and the system will coerce the value accordingly. Passing a float will also work and be
coerced. In strict typing mode, passing an integer to <literal>from()</literal> on a
string-backed enum (or vice versa) will result in a <classname>TypeError</classname>,
as will a float in all circumstances. All other parameter types will throw a TypeError
in both modes.
</para>
<programlisting role="php">
<![CDATA[
<?php
$record = get_stuff_from_database($id);
print $record['suit'];
$suit = Suit::from($record['suit']);
// Invalid data throws a ValueError: "X" is not a valid scalar value for enum "Suit"
print $suit->value;
$suit = Suit::tryFrom('A') ?? Suit::Spades;
// Invalid data returns null, so Suit::Spades is used instead.
print $suit->value;
?>
]]>
</programlisting>
<para>Manually defining a <literal>from()</literal> or <literal>tryFrom()</literal> method on a Backed Enum will result in a fatal error.</para>
</sect1>
<sect1 xml:id="language.enumerations.methods">
<title>Enumeration methods</title>
<para>
Enums (both Pure Enums and Backed Enums) may contain methods, and may implement interfaces.
If an Enum implements an interface, then any type check for that interface will also accept
all cases of that Enum.
</para>
<programlisting role="php">
<![CDATA[
<?php
interface Colorful
{
public function color(): string;
}
enum Suit implements Colorful
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
// Fulfills the interface contract.
public function color(): string
{
return match($this) {
Suit::Hearts, Suit::Diamonds => 'Red',
Suit::Clubs, Suit::Spades => 'Black',
};
}
// Not part of an interface; that's fine.
public function shape(): string
{
return "Rectangle";
}
}
function paint(Colorful $c)
{
/* ... */
}
paint(Suit::Clubs); // Works
print Suit::Diamonds->shape(); // prints "Rectangle"
?>
]]>
</programlisting>
<para>
In this example, all four instances of <literal>Suit</literal> have two methods,
<literal>color()</literal> and <literal>shape()</literal>. As far as calling code
and type checks are concerned, they behave exactly the same as any other object instance.
</para>
<para>
On a Backed Enum, the interface declaration goes after the backing type declaration.
</para>
<programlisting role="php">
<![CDATA[
<?php
interface Colorful
{
public function color(): string;
}
enum Suit: string implements Colorful
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
// Fulfills the interface contract.
public function color(): string
{
return match($this) {
Suit::Hearts, Suit::Diamonds => 'Red',
Suit::Clubs, Suit::Spades => 'Black',
};
}
}
?>
]]>
</programlisting>
<para>
Inside a method, the <literal>$this</literal> variable is defined and refers to the Case instance.
</para>
<para>
Methods may be arbitrarily complex, but in practice will usually return a static value or
&match; on <literal>$this</literal> to provide
different results for different cases.
</para>
<para>
Note that in this case it would be a better data modeling practice to also define a
<literal>SuitColor</literal> Enum Type with values Red and Black and return that instead.
However, that would complicate this example.
</para>
<para>
The above hierarchy is logically similar to the following class structure
(although this is not the actual code that runs):
</para>
<programlisting role="php">
<![CDATA[
<?php
interface Colorful
{
public function color(): string;
}
final class Suit implements UnitEnum, Colorful
{
public const Hearts = new self('Hearts');
public const Diamonds = new self('Diamonds');
public const Clubs = new self('Clubs');
public const Spades = new self('Spades');
private function __construct(public readonly string $name) {}
public function color(): string
{
return match($this) {
Suit::Hearts, Suit::Diamonds => 'Red',
Suit::Clubs, Suit::Spades => 'Black',
};
}
public function shape(): string
{
return "Rectangle";
}
public static function cases(): array
{
// Illegal method, because manually defining a cases() method on an Enum is disallowed.
// See also "Value listing" section.
}
}
?>
]]>
</programlisting>
<para>
Methods may be public, private, or protected, although in practice private and
protected are equivalent as inheritance is not allowed.
</para>
</sect1>
<sect1 xml:id="language.enumerations.static-methods">
<title>Enumeration static methods</title>
<para>
Enumerations may also have static methods. The use for static methods on the
enumeration itself is primarily for alternative constructors. E.g.:
</para>
<programlisting role="php">
<![CDATA[
<?php
enum Size
{
case Small;
case Medium;
case Large;
public static function fromLength(int $cm): static
{
return match(true) {
$cm < 50 => static::Small,
$cm < 100 => static::Medium,
default => static::Large,
};
}
}
?>
]]>
</programlisting>
<para>
Static methods may be public, private, or protected, although in practice private
and protected are equivalent as inheritance is not allowed.
</para>
</sect1>
<sect1 xml:id="language.enumerations.constants">
<title>Enumeration constants</title>
<para>
Enumerations may include constants, which may be public, private, or protected,
although in practice private and protected are equivalent as inheritance is not allowed.
</para>
<para>An enum constant may refer to an enum case:</para>
<programlisting role="php">
<![CDATA[
<?php
enum Size
{
case Small;
case Medium;
case Large;
public const Huge = self::Large;
}
?>
]]>
</programlisting>
</sect1>
<sect1 xml:id="language.enumerations.traits">
<title>Traits</title>
<para>Enumerations may leverage traits, which will behave the same as on classes.
The caveat is that traits <literal>use</literal>d in an enum must not contain properties.
They may only include methods and static methods. A trait with properties will
result in a fatal error.
</para>
<programlisting role="php">
<![CDATA[
<?php
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::Spades => 'Black',
};
}
}
?>
]]>
</programlisting>
</sect1>
<sect1 xml:id="language.enumerations.expressions">
<title>Enum values in constant expressions</title>
<para>
Because cases are represented as constants on the enum itself, they may be used as static
values in most constant expressions: property defaults, static variable defaults, parameter
defaults, global and class constant values. They may not be used in other enum case values, but
normal constants may refer to an enum case.
</para>
<para>
However, implicit magic method calls such as <classname>ArrayAccess</classname> on enums are not allowed in static
or constant definitions as we cannot absolutely guarantee that the resulting value is deterministic
or that the method invocation is free of side effects. Function calls, method calls, and
property access continue to be invalid operations in constant expressions.
</para>
<programlisting role="php">
<![CDATA[
<?php
// This is an entirely legal Enum definition.
enum Direction implements ArrayAccess
{
case Up;
case Down;
public function offsetExists($offset): bool
{
return false;
}
public function offsetGet($offset): mixed
{
return null;
}
public function offsetSet($offset, $value): void
{
throw new Exception();
}
public function offsetUnset($offset): void
{
throw new Exception();
}
}
class Foo
{
// This is allowed.
const DOWN = Direction::Down;
// This is disallowed, as it may not be deterministic.
const UP = Direction::Up['short'];
// Fatal error: Cannot use [] on enums in constant expression
}
// This is entirely legal, because it's not a constant expression.
$x = Direction::Up['short'];
var_dump("\$x is " . var_export($x, true));
$foo = new Foo();
?>
]]>
</programlisting>
</sect1>
<sect1 xml:id="language.enumerations.object-differences">
<title>Differences from objects</title>
<para>
Although Enums are built on classes and objects, they do not support all object-related functionality.
In particular, Enum cases are forbidden from having state.
</para>
<simplelist>
<member>Constructors and Destructors are forbidden.</member>
<member>Inheritance is not supported. Enums may not extend or be extended.</member>
<member>Static or object properties are not allowed.</member>
<member>Cloning an Enum case is not supported, as cases must be singleton instances.</member>
<member><link linkend="language.oop5.magic">Magic methods</link>, except for those listed below, are disallowed.</member>
<member>Enums must always be declared before they are used.</member>
</simplelist>
<para>The following object functionality is available, and behaves just as it does on any other object:</para>
<simplelist>
<member>Public, private, and protected methods.</member>
<member>Public, private, and protected static methods.</member>
<member>Public, private, and protected constants.</member>
<member>Enums may implement any number of interfaces.</member>
<member>
Enums and cases may have <link linkend="language.attributes">attributes</link> attached
to them. The <constant>TARGET_CLASS</constant> target
filter includes Enums themselves. The <constant>TARGET_CLASS_CONST</constant> target filter
includes Enum Cases.
</member>
<member>
<link linkend="object.call">__call</link>, <link linkend="object.callstatic">__callStatic</link>,
and <link linkend="object.invoke">__invoke</link> magic methods
</member>
<member><constant>__CLASS__</constant> and <constant>__FUNCTION__</constant> constants behave as normal</member>
</simplelist>
<para>
The <literal>::class</literal> magic constant on an Enum type evaluates to the type
name including any namespace, exactly the same as an object. The <literal>::class</literal>
magic constant on a Case instance also evaluates to the Enum type, as it is an
instance of that type.
</para>
<para>
Additionally, enum cases may not be instantiated directly with <literal>new</literal>, nor with
<methodname>ReflectionClass::newInstanceWithoutConstructor</methodname> in reflection. Both will result in an error.
</para>
<programlisting role="php">
<![CDATA[
<?php
$clovers = new Suit();
// Error: Cannot instantiate enum Suit
$horseshoes = (new ReflectionClass(Suit::class))->newInstanceWithoutConstructor()
// Error: Cannot instantiate enum Suit
?>
]]>
</programlisting>
</sect1>
<sect1 xml:id="language.enumerations.listing">
<title>Value listing</title>
<para>
Both Pure Enums and Backed Enums implement an internal interface named
<interfacename>UnitEnum</interfacename>. <literal>UnitEnum</literal> includes a static method
<literal>cases()</literal>. <literal>cases()</literal> returns a packed array of
all defined Cases in the order of declaration.
</para>
<programlisting role="php">
<![CDATA[
<?php
Suit::cases();
// Produces: [Suit::Hearts, Suit::Diamonds, Suit::Clubs, Suit::Spades]
?>
]]>
</programlisting>
<para>Manually defining a <literal>cases()</literal> method on an Enum will result in a fatal error.</para>
</sect1>
<sect1 xml:id="language.enumerations.serialization">
<title>Serialization</title>
<para>
Enumerations are serialized differently from objects. Specifically, they have a new serialization code,
<literal>"E"</literal>, that specifies the name of the enum case. The deserialization routine is then
able to use that to set a variable to the existing singleton value. That ensures that:
</para>
<programlisting role="php">
<![CDATA[
<?php
Suit::Hearts === unserialize(serialize(Suit::Hearts));
print serialize(Suit::Hearts);
// E:11:"Suit:Hearts";
?>
]]>
</programlisting>
<para>
On deserialization, if an enum and case cannot be found to match a serialized
value a warning will be issued and &false; returned.</para>
<para>
If a Pure Enum is serialized to JSON, an error will be thrown. If a Backed Enum
is serialized to JSON, it will be represented by its scalar value only, in the
appropriate type. The behavior of both may be overridden by implementing
<classname>JsonSerializable</classname>.
</para>
<para>For <function>print_r</function>, the output of an enum case is slightly different
from objects to minimize confusion.
</para>
<programlisting role="php">
<![CDATA[
<?php
enum Foo {
case Bar;
}
enum Baz: int {
case Beep = 5;
}
print_r(Foo::Bar);
print_r(Baz::Beep);
/* Produces
Foo Enum (
[name] => Bar
)
Baz Enum:int {
[name] => Beep
[value] => 5
}
*/
?>
]]>
</programlisting>
</sect1>
<sect1 xml:id="language.enumerations.object-differences.inheritance">
<title>Why enums aren't extendable</title>
<simpara>
Classes have contracts on their methods:
</simpara>
<programlisting role="php">
<![CDATA[
<?php
class A {}
class B extends A {}
function foo(A $a) {}
function bar(B $b) {
foo($b);
}
?>
]]>
</programlisting>
<simpara>
This code is type-safe, as B follows the contract of A, and through the magic of
co/contra-variance, any expectation one may have of the methods will be
preserved, exceptions excepted.
</simpara>
<simpara>
Enums have contracts on their cases, not methods:
</simpara>
<programlisting role="php">
<![CDATA[
<?php
enum ErrorCode {
case SOMETHING_BROKE;
}
function quux(ErrorCode $errorCode)
{
// When written, this code appears to cover all cases
match ($errorCode) {
ErrorCode::SOMETHING_BROKE => true,
}
}
?>
]]>
</programlisting>
<simpara>
The &match; statement in the function <code>quux</code> can be static analyzed to cover
all of the cases in ErrorCode.
</simpara>
<simpara>
But imagine it was allowed to extend enums:
</simpara>
<programlisting role="php">
<![CDATA[
<?php
// Thought experiment code where enums are not final.
// Note, this won't actually work in PHP.
enum MoreErrorCode extends ErrorCode {
case PEBKAC;
}
function fot(MoreErrorCode $errorCode) {
quux($errorCode);
}
fot(MoreErrorCode::PEBKAC);
?>
]]>
</programlisting>
<simpara>
Under normal inheritance rules, a class that extends another will pass
the type check.
</simpara>
<simpara>
The problem would be that the &match; statement in <code>quux()</code> no longer covers all
the cases. Because it doesn't know about <code>MoreErrorCode::PEBKAC</code> the match will throw an exception.
</simpara>
<simpara>
Because of this enums are final and can't be extended.
</simpara>
</sect1>
<sect1 xml:id="language.enumerations.examples">
&reftitle.examples;
<para>
<example>
<title>Basic limited values</title>
<programlisting role="php">
<![CDATA[
<?php
enum SortOrder
{
case Asc;
case Desc;
}
function query($fields, $filter, SortOrder $order = SortOrder::Asc)
{
/* ... */
}
?>
]]>
</programlisting>
<para>
The <literal>query()</literal> function can now proceed safe in the knowledge that
<literal>$order</literal> is guaranteed to be either <literal>SortOrder::Asc</literal>
or <literal>SortOrder::Desc</literal>. Any other value would have resulted in a
<classname>TypeError</classname>, so no further error checking or testing is needed.
</para>
</example>
</para>
<para>
<example>
<title>Advanced exclusive values</title>
<programlisting role="php">
<![CDATA[
<?php
enum UserStatus: string
{
case Pending = 'P';
case Active = 'A';
case Suspended = 'S';
case CanceledByUser = 'C';
public function label(): string
{
return match($this) {
static::Pending => 'Pending',
static::Active => 'Active',
static::Suspended => 'Suspended',
static::CanceledByUser => 'Canceled by user',
};
}
}
?>
]]>
</programlisting>
<para>
In this example, a user's status may be one of, and exclusively, <literal>UserStatus::Pending</literal>,
<literal>UserStatus::Active</literal>, <literal>UserStatus::Suspended</literal>, or
<literal>UserStatus::CanceledByUser</literal>. A function can type a parameter against
<literal>UserStatus</literal> and then only accept those four values, period.
</para>
<para>
All four values have a <literal>label()</literal> method, which returns a human-readable string.
That string is independent of the "machine name" scalar equivalent string, which can be used in,
for example, a database field or an HTML select box.
</para>
<programlisting role="php">
<![CDATA[
<?php
foreach (UserStatus::cases() as $case) {
printf('<option value="%s">%s</option>\n', $case->value, $case->label());
}
?>
]]>
</programlisting>
</example>
</para>
</sect1>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|