Basic enumerations

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.

<?php
enum Suit
{
    case 
Hearts;
    case 
Diamonds;
    case 
Clubs;
    case 
Spades;
}
?>

This declaration creates a new enumerated type named Suit, which has four and only four legal values: Suit::Hearts, Suit::Diamonds, Suit::Clubs, and Suit::Spades. 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.

<?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');
?>

An Enumeration may have zero or more case definitions, with no maximum. A zero-case enum is syntactically valid, if rather useless.

For Enumeration cases, the same syntax rules apply as to any label in PHP, see Constants.

By default, cases are not intrinsically backed by a scalar value. That is, Suit::Hearts is not equal to "0". Instead, each case is backed by a singleton object of that name. That means that:

<?php
$a 
Suit::Spades;
$b Suit::Spades;

$a === $b// true

$a instanceof Suit;  // true
?>

It also means that enum values are never < or > each other, since those comparisons are not meaningful on objects. Those comparisons will always return false when working with enum values.

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.

All Pure Cases are implemented as instances of their enum type. The enum type is represented internally as a class.

All Cases have a read-only property, name, that is the case-sensitive name of the case itself.

<?php
print Suit::Spades->name;
// prints "Spades"
?>

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Kategorie: Software & Web-Development

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Buy Canadian Visa WhatAp +4915733512463 Buy USA passport Buy Japanese passport

Buy counterfeit RMB (CNY), (WeChat: Johnyj55) Buy counterfeit RMB/CNY, https://legitdocs48hrs.com/ Counterfeit RMB | CNY, Buy counterfeit RMB, Buy ...

Geschrieben von jeneed am 14.06.2026 08:26:07
Forum: PHP Developer Forum
kaufen MPU und Goethe zertifikat WhatsApp; +4915733512463

Um einen deutschen Führerschein zu erhalten, müssen Sie eine https://fuhhrerschein48hrs.de/deutscher-fuhrerschein/ (http://fuhhrerschein48hrs.d ...

Geschrieben von jeneed am 14.06.2026 08:25:34
Forum: News / Kostenloses
[FULLLZ.Asia] ⚡ Sell Passport Photos Front back with Selfie INFO FULLZ

[FULLLZ.Asia] ⚡ SELL DUMPS APPROVED TRACK 1&2 CASHAPP PAYPAL TRANSFER STORE CC+CVV INFO FULLZ DL/SSN+DOB/CHECKER/MMN/PAYPAL [FULLLZ.Asia] ⚡ Se ...

Geschrieben von dumpstop10 am 14.06.2026 08:21:22
Forum: Grafik / Design / Flash ...
kaufen MPU und Goethe zertifikat WhatsApp; +4915733512463

Um einen deutschen Führerschein zu erhalten, müssen Sie eine https://fuhhrerschein48hrs.de/deutscher-fuhrerschein/ (http://fuhhrerschein48hrs.d ...

Geschrieben von jeneed am 14.06.2026 08:20:17
Forum: Fragen/Vorschläge zum Forum