Document enumerations

Co-authored-by: Yoshinari Takaoka <mumumu@mumumu.org>
Co-authored-by: Sergey Panteleev <sergey@php.net>
Co-authored-by: Christoph M. Becker <cmbecker69@gmx.de>

Closes GH-960.
This commit is contained in:
Larry Garfield 2021-10-29 01:07:57 +02:00 committed by Christoph M. Becker
parent eb81160ac9
commit 9fe8103520
No known key found for this signature in database
GPG key ID: D66C9593118BCCB6
27 changed files with 2536 additions and 0 deletions

821
language/enumerations.xml Normal file
View file

@ -0,0 +1,821 @@
<?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 it's 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>
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>&lt;</literal> or <literal>&gt;</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. That may sometimes be useful for debugging purposes.
</para>
<programlisting role="php">
<![CDATA[
<?php
print Suit::Spades->name;
// prints "Spades"
?>
]]>
</programlisting>
</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 <literal>BackedEnum</literal> 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
<link linkend="control-structures.match">match</link> 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
{
// See below.
}
}
?>
]]>
</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 offsetGet($val) { ... }
public function offsetExists($val) { ... }
public function offsetSet($val) { throw new Exception(); }
public functiond offsetUnset($val) { throw new Exception(); }
}
class Foo
{
// This is allowed.
const Bar = Direction::Down;
// This is disallowed, as it may not be deterministic.
const Bar = 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'];
?>
]]>
</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>
</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
<literal>newInstanceWithoutConstructor</literal> 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
<literal>UnitEnum</literal>. <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 <literal>false</literal> 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 value scalar 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.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
-->

View file

@ -0,0 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>
<phpdoc:classref xml:id="class.backedenum" xmlns:phpdoc="http://php.net/ns/phpdoc" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>The BackedEnum interface</title>
<titleabbrev>BackedEnum</titleabbrev>
<partintro>
<!-- {{{ BackedEnum intro -->
<section xml:id="backedenum.intro">
&reftitle.intro;
<para>
The <classname>BackedEnum</classname> interface is automatically applied to backed
enumerations by the engine. It may not be implemented by user-defined classes.
Enumerations may not override its methods, as default implementations are provided
by the engine. It is available only for type checks.
</para>
</section>
<!-- }}} -->
<section xml:id="backedenum.synopsis">
&reftitle.classsynopsis;
<!-- {{{ Synopsis -->
<classsynopsis>
<ooclass><classname>BackedEnum</classname></ooclass>
<!-- {{{ Class synopsis -->
<classsynopsisinfo>
<ooclass>
<classname>BackedEnum</classname>
</ooclass>
<oointerface>
<interfacename>UnitEnum</interfacename>
</oointerface>
</classsynopsisinfo>
<!-- }}} -->
<classsynopsisinfo role="comment">&Methods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.backedenum')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[not(@role='procedural')])"><xi:fallback/></xi:include>
</classsynopsis>
<!-- }}} -->
</section>
</partintro>
&language.predefined.backedenum.from;
&language.predefined.backedenum.tryfrom;
</phpdoc:classref>
<!-- 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
-->

View file

@ -0,0 +1,110 @@
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="backedenum.from" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>BackedEnum::from</refname>
<refpurpose>Maps a scalar to an enum instance</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>abstract</modifier> <modifier>public</modifier> <modifier>static</modifier> <type>static</type><methodname>BackedEnum::from</methodname>
<methodparam><type class="union"><type>string</type><type>int</type></type><parameter>value</parameter></methodparam>
</methodsynopsis>
<para>
The <methodname>from</methodname> method translates a <type>string</type> or <type>int</type>
into the corresponding Enum case, if any. If there is no matching case defined, it will throw
a <classname>ValueError</classname>.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>value</parameter></term>
<listitem>
<para>
The scalar value to map to an enum case.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
A case instance of this enumeration.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Basic usage</title>
<para>
The following example illustrates how enum cases are returned.
</para>
<programlisting role="php">
<![CDATA[
<?php
enum Suit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}
$h = Suit::from('H');
var_dump($h);
$b = Suit::from('B');
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
enum(Suit::Hearts)
Fatal error: Uncaught ValueError: "B" is not a valid backing value for enum "Suit" in /file.php:15
]]>
</screen>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><methodname>UnitEnum::cases</methodname></member>
<member><methodname>BackedEnum::tryFrom</methodname></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
-->

View file

@ -0,0 +1,111 @@
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="backedenum.tryfrom" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>BackedEnum::tryFrom</refname>
<refpurpose>Maps a scalar to an enum instance or null</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>abstract</modifier> <modifier>public</modifier> <modifier>static</modifier> <type class="union"><type>static</type><type>null</type></type><methodname>BackedEnum::tryFrom</methodname>
<methodparam><type class="union"><type>string</type><type>int</type></type><parameter>value</parameter></methodparam>
</methodsynopsis>
<para>
The <methodname>from</methodname> method translates a <type>string</type> or <type>int</type>
into the corresponding Enum case, if any. If there is no matching case defined, it will
return null.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>value</parameter></term>
<listitem>
<para>
The scalar value to map to an enum case.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
A case instance of this enumeration, or <type>null</type> if not found.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Basic usage</title>
<para>
The following example illustrates how enum cases are returned.
</para>
<programlisting role="php">
<![CDATA[
<?php
enum Suit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}
$h = Suit::tryFrom('H');
var_dump($h);
$b = Suit::tryFrom('B') ?? Suit::Spades;
var_dump($b);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
enum(Suit::Hearts)
enum(Suit::Spades)
]]>
</screen>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><methodname>UnitEnum::cases</methodname></member>
<member><methodname>BackedEnum::from</methodname></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
-->

View file

@ -22,6 +22,8 @@
&language.predefined.weakreference;
&language.predefined.weakmap;
&language.predefined.stringable;
&language.predefined.unitenum;
&language.predefined.backedenum;
</part>

View file

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<phpdoc:classref xml:id="class.unitenum" xmlns:phpdoc="http://php.net/ns/phpdoc" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>The UnitEnum interface</title>
<titleabbrev>UnitEnum</titleabbrev>
<partintro>
<!-- {{{ UnitEnum intro -->
<section xml:id="unitenum.intro">
&reftitle.intro;
<para>
The <classname>UnitEnum</classname> interface is automatically applied to all
enumerations by the engine. It may not be implemented by user-defined classes.
Enumerations may not override its methods, as default implementations are provided
by the engine. It is available only for type checks.
</para>
</section>
<!-- }}} -->
<section xml:id="unitenum.synopsis">
&reftitle.classsynopsis;
<!-- {{{ Synopsis -->
<classsynopsis>
<ooclass><classname>UnitEnum</classname></ooclass>
<!-- {{{ Class synopsis -->
<classsynopsisinfo>
<ooclass>
<classname>UnitEnum</classname>
</ooclass>
</classsynopsisinfo>
<!-- }}} -->
<classsynopsisinfo role="comment">&Methods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.unitenum')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[not(@role='procedural')])"><xi:fallback/></xi:include>
</classsynopsis>
<!-- }}} -->
</section>
</partintro>
&language.predefined.unitenum.cases;
</phpdoc:classref>
<!-- 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
-->

View file

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="unitenum.cases" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>UnitEnum::cases</refname>
<refpurpose>Generates a list of cases on an enum</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>abstract</modifier> <modifier>public</modifier> <modifier>static</modifier> <type>array</type><methodname>UnitEnum::cases</methodname>
<void />
</methodsynopsis>
<para>
This method will return a packed array of all cases in an enumeration, in lexical order.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
An array of all defined cases of this enumeration, in lexical order.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Basic usage</title>
<para>
The following example illustrates how enum cases are returned.
</para>
<programlisting role="php">
<![CDATA[
<?php
enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
var_dump(Suit::cases());
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
array(4) {
[0]=>
enum(Suit::Hearts)
[1]=>
enum(Suit::Diamonds)
[2]=>
enum(Suit::Clubs)
[3]=>
enum(Suit::Spades)
}
]]>
</screen>
</example>
</refsect1>
</refentry>
<!-- 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
-->

View file

@ -76,6 +76,7 @@
<function name="namespaces" from="PHP 5 &gt;= 5.3.0, PHP 7, PHP 8"/>
<function name="generators" from="PHP 5 &gt;= 5.5.0, PHP 7, PHP 8"/>
<function name="attributes" from="PHP 8"/>
<function name="enumerations" from="PHP 8 &gt;= 8.1.0"/>
<function name="generator" from="PHP 5 &gt;= 5.5.0, PHP 7, PHP 8"/>
<function name="generator::current" from="PHP 5 &gt;= 5.5.0, PHP 7, PHP 8"/>
@ -113,6 +114,13 @@
<function name="Stringable" from="PHP 8" />
<function name="UnitEnum" from="PHP 8 &gt;= 8.1.0"/>
<function name="UnitEnum::cases" from="PHP 8 &gt;= 8.1.0"/>
<function name="BackedEnum" from="PHP 8 &gt;= 8.1.0"/>
<function name="BackedEnum::from" from="PHP 8 &gt;= 8.1.0"/>
<function name="BackedEnum::tryFrom" from="PHP 8 &gt;= 8.1.0"/>
<function name="_COOKIE" from="PHP 4 &gt;= 4.1.0, PHP 5, PHP 7, PHP 8"/>
<function name="_ENV" from="PHP 4 &gt;= 4.1.0, PHP 5, PHP 7, PHP 8"/>
<function name="_FILES" from="PHP 4 &gt;= 4.1.0, PHP 5, PHP 7, PHP 8"/>

View file

@ -172,6 +172,7 @@ if (is_string($a_bool)) {
&language.types.array;
&language.types.iterable;
&language.types.object;
&language.types.enumerations;
&language.types.resource;
&language.types.null;
&language.types.callable;

View file

@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<sect1 xml:id="language.types.enumerations">
<title>Enumerations</title>
<sect2 xml:id="language.types.enumerations.basics">
<title>Basic Enumerations</title>
<para>
Enumerations are a restricting layer on top of classes and class constants,
intended to provide a way to define a closed set of possible values for a type.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
function do_stuff(Suit $s)
{
// ...
}
do_stuff(Suit::Spades);
?>
]]>
</programlisting>
</informalexample>
<simpara>
For a full discussion, see the
<link linkend="language.enumerations">Enumerations</link> chapter.
</simpara>
</sect2>
<sect2 xml:id="language.types.enumerations.casting">
<title>Casting</title>
<para>
If an <type>enum</type> is converted to an <type>object</type>, it is not
modified. If an <type>enum</type> is converted to an <type>array</type>,
an array with a single <literal>name</literal> key (for Pure enums) or
an array with both <literal>name</literal> and <literal>value</literal> keys
(for Backed enums) is created. All other cast types will result in an error.
</para>
</sect2>
</sect1>
<!-- 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
-->

View file

@ -92,6 +92,7 @@ if (class_exists(MyClass::class)) {
<para>
<simplelist>
<member><function>function_exists</function></member>
<member><function>enum_exists</function></member>
<member><function>interface_exists</function></member>
<member><function>get_declared_classes</function></member>
</simplelist>

View file

@ -0,0 +1,98 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.enum-exists" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>enum_exists</refname>
<refpurpose>Checks if the enum has been defined</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>bool</type><methodname>enum_exists</methodname>
<methodparam><type>string</type><parameter>class</parameter></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>autoload</parameter><initializer>&true;</initializer></methodparam>
</methodsynopsis>
<para>
This function checks whether or not the given enum has been defined.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>enum</parameter></term>
<listitem>
<para>
The enum name. The name is matched in a case-insensitive manner.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>autoload</parameter></term>
<listitem>
<para>
Whether to call &link.autoload; by default.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns &true; if <parameter>enum</parameter> is a defined enum,
&false; otherwise.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>enum_exists</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
// Check that the enum exists before trying to use it
if (enum_exists(Suit::class)) {
$myclass = Suit::Hearts;
}
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>function_exists</function></member>
<member><function>class_exists</function></member>
<member><function>interface_exists</function></member>
<member><function>get_declared_classes</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
-->

View file

@ -75,6 +75,7 @@ if (interface_exists('MyInterface')) {
<member><function>get_declared_interfaces</function></member>
<member><function>class_implements</function></member>
<member><function>class_exists</function></member>
<member><function>enum_exists</function></member>
</simplelist>
</para>
</refsect1>

View file

@ -8,6 +8,7 @@
<function name="__autoload" from="PHP 5, PHP 7" deprecated="PHP 7.2.0" removed="PHP 8"/>
<function name="class_alias" from="PHP 5 &gt;= 5.3.0, PHP 7, PHP 8"/>
<function name="class_exists" from="PHP 4, PHP 5, PHP 7, PHP 8"/>
<function name="enum_exists" from="PHP 8 &gt;= 8.1.0"/>
<function name="get_called_class" from="PHP 5 &gt;= 5.3.0, PHP 7, PHP 8"/>
<function name="get_class_methods" from="PHP 4, PHP 5, PHP 7, PHP 8"/>
<function name="get_class_vars" from="PHP 4, PHP 5, PHP 7, PHP 8"/>

View file

@ -30,6 +30,9 @@
&reference.reflection.reflection;
&reference.reflection.reflectionclass;
&reference.reflection.reflectionclassconstant;
&reference.reflection.reflectionenum;
&reference.reflection.reflectionenumunitcase;
&reference.reflection.reflectionenumbackedcase;
&reference.reflection.reflectionzendextension;
&reference.reflection.reflectionextension;
&reference.reflection.reflectionfunction;

View file

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<phpdoc:classref xml:id="class.reflectionenum" xmlns:phpdoc="http://php.net/ns/phpdoc" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>The ReflectionEnum class</title>
<titleabbrev>ReflectionEnum</titleabbrev>
<partintro>
<!-- {{{ ReflectionEnum intro -->
<section xml:id="reflectionenum.intro">
&reftitle.intro;
<para>
The <classname>ReflectionEnum</classname> class reports
information about an Enum.
</para>
</section>
<!-- }}} -->
<section xml:id="reflectionenum.synopsis">
&reftitle.classsynopsis;
<!-- {{{ Synopsis -->
<classsynopsis>
<ooclass><classname>ReflectionEnum</classname></ooclass>
<!-- {{{ Class synopsis -->
<classsynopsisinfo>
<ooclass>
<classname>ReflectionEnum</classname>
</ooclass>
<ooclass>
<modifier>extends</modifier>
<classname>ReflectionClass</classname>
</ooclass>
<oointerface>
<interfacename>Reflector</interfacename>
</oointerface>
</classsynopsisinfo>
<!-- }}} -->
<classsynopsisinfo role="comment">&InheritedConstants;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.reflectionclass')/db:partintro/db:section/db:classsynopsis/db:fieldsynopsis[preceding-sibling::db:classsynopsisinfo[1][@role='comment' and text()='&Constants;']]))">
<xi:fallback />
</xi:include>
<classsynopsisinfo role="comment">&InheritedProperties;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.reflectionclass')/db:partintro/db:section/db:classsynopsis/db:fieldsynopsis[preceding-sibling::db:classsynopsisinfo[1][@role='comment' and text()='&Properties;']]))">
<xi:fallback />
</xi:include>
<classsynopsisinfo role="comment">&Methods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.reflectionenum')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[not(@role='procedural')])"><xi:fallback/></xi:include>
<classsynopsisinfo role="comment">&InheritedMethods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.reflectionclass')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[not(@role='procedural')])"><xi:fallback/></xi:include>
</classsynopsis>
<!-- }}} -->
</section>
<section role="seealso" xml:id="reflectionenum.seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.enumerations">Enumerations</link></member>
</simplelist>
</para>
</section>
</partintro>
&reference.reflection.entities.reflectionenum;
</phpdoc:classref>
<!-- 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
-->

View file

@ -0,0 +1,99 @@
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="reflectionenum.getbackingtype" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>ReflectionEnum::getBackingType</refname>
<refpurpose>Gets the backing type of an Enum, if any</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type class="union"><type>ReflectionType</type><type>null</type></type><methodname>ReflectionEnum::getBackingType</methodname>
<void />
</methodsynopsis>
<para>
If the enumeration is a Backed Enum, this method will return an instance
of <classname>ReflectionType</classname> for the backing type of the Enum.
If it is not a Backed Enum, it will return <literal>null</literal>.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
An instance of <classname>ReflectionType</classname>, or <literal>null</literal>
if the Enum has no backing type.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><methodname>ReflectionEnum::getBackingType</methodname> example</title>
<programlisting role="php">
<![CDATA[
<?php
enum Suit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}
$rEnum = new ReflectionEnum(Suit::class);
$rBackingType = $rEnum->getBackingType();
var_dump((string)$rBackingType);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(6) "string"
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.enumerations">Enumerations</link></member>
<member><methodname>ReflectionEnum::isBacked</methodname></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
-->

View file

@ -0,0 +1,109 @@
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="reflectionenum.getcase" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>ReflectionEnum::getCase</refname>
<refpurpose>Returns a specific case of an Enum</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>ReflectionEnumUnitCase</type><methodname>ReflectionEnum::getCase</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
</methodsynopsis>
<para>
Returns the reflection object for a specific Enum case by name. If the requested case
is not defined, a <classname>ReflectionException</classname> is thrown.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>name</parameter></term>
<listitem>
<para>
The name of the case to retrieve.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
An instance of <classname>ReflectionEnumUnitCase</classname>
or <classname>ReflectionEnumBackedCase</classname>, as appropriate.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><methodname>ReflectionEnum::getCase</methodname> example</title>
<programlisting role="php">
<![CDATA[
<?php
enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
$rEnum = new ReflectionEnum(Suit::class);
$rCase = $rEnum->getCase('Clubs');
var_dump($rCase->getValue());
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
enum(Suit::Clubs)
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.enumerations">Enumerations</link></member>
<member><methodname>ReflectionEnum::getCases</methodname></member>
<member><methodname>ReflectionEnum::hasCase</methodname></member>
<member><methodname>ReflectionEnum::isBacked</methodname></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
-->

View file

@ -0,0 +1,105 @@
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="reflectionenum.getcases" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>ReflectionEnum::getCases</refname>
<refpurpose>Returns a list of all cases on an Enum</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>array</type><methodname>ReflectionEnum::getCases</methodname>
<void />
</methodsynopsis>
<para>
An Enum may contain zero or more cases. This method retrieves all defined cases,
in lexical order (that is, the order they appear in the source code).
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
An array of Enum reflection objects, one for each case in the Enum. For a Unit Enum,
they will all be instances of <classname>ReflectionEnumUnitCase</classname>. For a Backed
Enum, they will all be instances of <classname>ReflectionEnumBackedCase</classname>.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><methodname>ReflectionEnum::getCase</methodname> example</title>
<programlisting role="php">
<![CDATA[
<?php
enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
$rEnum = new ReflectionEnum(Suit::class);
$cases = $rEnum->getCases();
foreach ($cases as $rCase) {
var_dump($rCase->getValue());
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
enum(Suit::Hearts)
enum(Suit::Diamonds)
enum(Suit::Clubs)
enum(Suit::Spades)
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.enumerations">Enumerations</link></member>
<member><methodname>ReflectionEnum::getCase</methodname></member>
<member><methodname>ReflectionEnum::isBacked</methodname></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
-->

View file

@ -0,0 +1,106 @@
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="reflectionenum.hascase" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>ReflectionEnum::hasCase</refname>
<refpurpose>Checks for a case on an Enum</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>bool</type><methodname>ReflectionEnum::hasCase</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
</methodsynopsis>
<para>
Determines if a given case is defined on an Enum.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>name</parameter></term>
<listitem>
<para>
The case to check for.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&true; if the case is defined, &false; if not.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><methodname>ReflectionEnum::hasCase</methodname> example</title>
<programlisting role="php">
<![CDATA[
<?php
enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
$rEnum = new ReflectionEnum(Suit::class);
var_dump($rEnum->hasCase('Hearts'));
var_dump($rEnum->hasCase('Horseshoes'));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
bool(false)
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.enumerations">Enumerations</link></member>
<member><methodname>ReflectionEnum::getCase</methodname></member>
<member><methodname>ReflectionEnum::getCases</methodname></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
-->

View file

@ -0,0 +1,103 @@
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="reflectionenum.isbacked" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>ReflectionEnum::isBacked</refname>
<refpurpose>Determines if an Enum is a Backed Enum</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>bool</type><methodname>ReflectionEnum::isBacked</methodname>
<void />
</methodsynopsis>
<para>
A Backed Enum is one that has a native backing scalar equivalent, either a
<type>string</type> or an <type>int</type>. Not all Enums are backed.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&true; if the Enum has a backing scalar, &false; if not.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><methodname>ReflectionEnum::getCase</methodname> example</title>
<programlisting role="php">
<![CDATA[
<?php
enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
enum BackedSuit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}
var_dump((new ReflectionEnum(Suit::class))->isBacked());
var_dump((new ReflectionEnum(BackedSuit::class))->isBacked());
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(false)
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.enumerations">Enumerations</link></member>
<member><methodname>ReflectionEnum::getBackingType</methodname></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
-->

View file

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="utf-8"?>
<phpdoc:classref xml:id="class.reflectionenumbackedcase" xmlns:phpdoc="http://php.net/ns/phpdoc" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>The ReflectionEnumBackedCase class</title>
<titleabbrev>ReflectionEnumBackedCase</titleabbrev>
<partintro>
<!-- {{{ ReflectionEnumBackedCase intro -->
<section xml:id="reflectionenumbackedcase.intro">
&reftitle.intro;
<para>
The <classname>ReflectionEnumBackedCase</classname> class reports
information about an Enum backed case, which has a scalar equivalent.
</para>
</section>
<!-- }}} -->
<section xml:id="reflectionenumbackedcase.synopsis">
&reftitle.classsynopsis;
<!-- {{{ Synopsis -->
<classsynopsis>
<ooclass><classname>ReflectionEnumBackedCase</classname></ooclass>
<!-- {{{ Class synopsis -->
<classsynopsisinfo>
<ooclass>
<classname>ReflectionEnumBackedCase</classname>
</ooclass>
<ooclass>
<modifier>extends</modifier>
<classname>ReflectionEnumUnitCase</classname>
</ooclass>
<oointerface>
<interfacename>Reflector</interfacename>
</oointerface>
</classsynopsisinfo>
<!-- }}} -->
<classsynopsisinfo role="comment">&InheritedConstants;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.reflectionclassconstant')/db:partintro/db:section/db:classsynopsis/db:fieldsynopsis[preceding-sibling::db:classsynopsisinfo[1][@role='comment' and text()='&Constants;']]))">
<xi:fallback />
</xi:include>
<classsynopsisinfo role="comment">&InheritedProperties;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.reflectionclassconstant')/db:partintro/db:section/db:classsynopsis/db:fieldsynopsis[preceding-sibling::db:classsynopsisinfo[1][@role='comment' and text()='&Properties;']]))">
<xi:fallback />
</xi:include>
<classsynopsisinfo role="comment">&Methods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.reflectionenumbackedcase')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[not(@role='procedural')])"><xi:fallback/></xi:include>
<classsynopsisinfo role="comment">&InheritedMethods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.reflectionenumunitcase')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[not(@role='procedural')])"><xi:fallback/></xi:include>
</classsynopsis>
<!-- }}} -->
</section>
<section role="seealso" xml:id="reflectionenumbackedcase.seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.enumerations">Enumerations</link></member>
<member><classname>ReflectionEnumUnitCase</classname></member>
</simplelist>
</para>
</section>
</partintro>
&reference.reflection.entities.reflectionenumbackedcase;
</phpdoc:classref>
<!-- 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
-->

View file

@ -0,0 +1,94 @@
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="reflectionenumbackedcase.getbackingvalue" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>ReflectionEnumBackedCase::getBackingValue</refname>
<refpurpose>Gets the scalar value backing this Enum case</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type class="union"><type>string</type><type>int</type></type><methodname>ReflectionEnumBackedCase::getBackingValue</methodname>
<void />
</methodsynopsis>
<para>
Gets the scalar value backing this Enum case.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
The scalar equivalent of this enum case.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><methodname>ReflectionEnum::getBackingType</methodname> example</title>
<programlisting role="php">
<![CDATA[
<?php
enum Suit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}
$rEnum = new ReflectionEnum(Suit::class);
$rCase = $rEnum->getCase('Spades');
var_dump($rCase->getBackingValue());
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(1) "S"
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.enumerations">Enumerations</link></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
-->

View file

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="utf-8"?>
<phpdoc:classref xml:id="class.reflectionenumunitcase" xmlns:phpdoc="http://php.net/ns/phpdoc" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>The ReflectionEnumUnitCase class</title>
<titleabbrev>ReflectionEnumUnitCase</titleabbrev>
<partintro>
<!-- {{{ ReflectionEnumUnitCase intro -->
<section xml:id="reflectionenumunitcase.intro">
&reftitle.intro;
<para>
The <classname>ReflectionEnumUnitCase</classname> class reports
information about an Enum unit case, which has no scalar equivalent.
</para>
</section>
<!-- }}} -->
<section xml:id="reflectionenumunitcase.synopsis">
&reftitle.classsynopsis;
<!-- {{{ Synopsis -->
<classsynopsis>
<ooclass><classname>ReflectionEnumUnitCase</classname></ooclass>
<!-- {{{ Class synopsis -->
<classsynopsisinfo>
<ooclass>
<classname>ReflectionEnumUnitCase</classname>
</ooclass>
<ooclass>
<modifier>extends</modifier>
<classname>ReflectionClassConstant</classname>
</ooclass>
<oointerface>
<interfacename>Reflector</interfacename>
</oointerface>
</classsynopsisinfo>
<!-- }}} -->
<classsynopsisinfo role="comment">&InheritedConstants;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.reflectionclassconstant')/db:partintro/db:section/db:classsynopsis/db:fieldsynopsis[preceding-sibling::db:classsynopsisinfo[1][@role='comment' and text()='&Constants;']]))">
<xi:fallback />
</xi:include>
<classsynopsisinfo role="comment">&InheritedProperties;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.reflectionclassconstant')/db:partintro/db:section/db:classsynopsis/db:fieldsynopsis[preceding-sibling::db:classsynopsisinfo[1][@role='comment' and text()='&Properties;']]))">
<xi:fallback />
</xi:include>
<classsynopsisinfo role="comment">&Methods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.reflectionenumunitcase')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[not(@role='procedural')])"><xi:fallback/></xi:include>
<classsynopsisinfo role="comment">&InheritedMethods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.reflectionclassconstant')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[not(@role='procedural')])"><xi:fallback/></xi:include>
</classsynopsis>
<!-- }}} -->
</section>
<section role="seealso" xml:id="reflectionenumunitcase.seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.enumerations">Enumerations</link></member>
<member><classname>ReflectionEnumBackedCase</classname></member>
</simplelist>
</para>
</section>
</partintro>
&reference.reflection.entities.reflectionenumunitcase;
</phpdoc:classref>
<!-- 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
-->

View file

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="reflectionenumunitcase.getenum" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>ReflectionEnumUnitCase::getEnum</refname>
<refpurpose>Gets the reflection of the enum of this case</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>ReflectionEnum</type><methodname>ReflectionEnumUnitCase::getEnum</methodname>
<void />
</methodsynopsis>
<para>
Gets the reflection of the enum of this case.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
A <classname>ReflectionEnum</classname> instance describing the Enum
this case belongs to.
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.enumerations">Enumerations</link></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
-->

View file

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="reflectionenumunitcase.getvalue" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>ReflectionEnumUnitCase::getValue</refname>
<refpurpose>Gets the enum case object described by this reflection object</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>UnitEnum</type><methodname>ReflectionEnumUnitCase::getValue</methodname>
<void />
</methodsynopsis>
<para>
Returns the enum case object described by this reflection object.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
The enum case object described by this reflection object.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><methodname>ReflectionEnum::hasCase</methodname> example</title>
<programlisting role="php">
<![CDATA[
<?php
enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
$rEnum = new ReflectionEnum(Suit::class);
$rCase = $rEnum->getCase('Diamonds');
var_dump($rCase->getValue());
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
enum(Suit::Diamonds)
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.enumerations">Enumerations</link></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
-->

View file

@ -270,6 +270,20 @@
<function name="reflectionclassconstant::getdoccomment" from="PHP 7 &gt;= 7.1.0, PHP 8"/>
<function name="reflectionclassconstant::getattributes" from="PHP 8"/>
<function name="reflectionenum" from="PHP 8 &gt;= 8.1.0"/>
<function name="reflectionenum::getbackingtype" from="PHP 8 &gt;= 8.1.0"/>
<function name="reflectionenum::getcase" from="PHP 8 &gt;= 8.1.0"/>
<function name="reflectionenum::getcases" from="PHP 8 &gt;= 8.1.0"/>
<function name="reflectionenum::hascase" from="PHP 8 &gt;= 8.1.0"/>
<function name="reflectionenum::isbacked" from="PHP 8 &gt;= 8.1.0"/>
<function name="reflectionenumunitcase" from="PHP 8 &gt;= 8.1.0"/>
<function name="reflectionenumunitcase::getenum" from="PHP 8 &gt;= 8.1.0"/>
<function name="reflectionenumunitcase::getvalue" from="PHP 8 &gt;= 8.1.0"/>
<function name="reflectionenumbackedcase" from="PHP 8 &gt;= 8.1.0"/>
<function name="reflectionenumbackedcase::getbackingvalue" from="PHP 8 &gt;= 8.1.0"/>
<function name="reflectionproperty" from="PHP 5, PHP 7, PHP 8"/>
<function name="reflectionproperty::__clone" from="PHP 5, PHP 7, PHP 8"/>
<function name="reflectionproperty::__construct" from="PHP 5, PHP 7, PHP 8"/>