> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP Backed Enum

A backed enum in PHP is an enumeration whose cases are explicitly backed by a scalar value, specifically either an `int` or a `string`. Introduced in PHP 8.1, backed enums allow enumeration cases to be serialized and deserialized by associating a unique, literal scalar value with each case.

## Syntax and Declaration

To create a backed enum, you must declare the scalar type immediately following the enum name. Every case within the enum must be explicitly assigned a value matching that type.

```php theme={"dark"}
// String-backed enum
enum HttpMethod: string {
    case GET = 'GET';
    case POST = 'POST';
    case PUT = 'PUT';
    case DELETE = 'DELETE';
}

// Integer-backed enum
enum HttpStatusCode: int {
    case OK = 200;
    case CREATED = 201;
    case NOT_FOUND = 404;
}
```

## Technical Constraints

1. **Strict Typing:** The backing type must be exactly `int` or `string`. Union types (e.g., `int|string`) are strictly prohibited.
2. **Homogeneity:** An enum cannot mix integer and string values. All cases must resolve to the declared backing type.
3. **Uniqueness:** Every case value must be unique within the enumeration. Two cases cannot share the same scalar value.
4. **Literal Values:** The assigned values must be literals or literal expressions. Variables, function calls, or dynamic expressions are not allowed.

## Accessing the Backing Value

Backed enums expose their underlying scalar value via a read-only public property named `value`.

```php theme={"dark"}
$method = HttpMethod::POST;
echo $method->value; // Outputs: POST

$status = HttpStatusCode::NOT_FOUND;
echo $status->value; // Outputs: 404
```

## Deserialization Methods

Backed enums implicitly implement the `BackedEnum` interface, which provides two static methods for converting a scalar value back into an enum instance.

### `from()`

The `from()` method takes a scalar value and returns the corresponding enum case. If the value does not exist within the enum, it throws a `ValueError`.

```php theme={"dark"}
$method = HttpMethod::from('GET'); 
// Returns HttpMethod::GET

$invalid = HttpMethod::from('PATCH'); 
// Throws ValueError: "PATCH" is not a valid backing value for enum "HttpMethod"
```

### `tryFrom()`

The `tryFrom()` method operates similarly to `from()`, but instead of throwing an exception on failure, it returns `null`. This is the preferred method when handling untrusted input.

```php theme={"dark"}
$status = HttpStatusCode::tryFrom(200); 
// Returns HttpStatusCode::OK

$invalidStatus = HttpStatusCode::tryFrom(500); 
// Returns null
```

## The `BackedEnum` Interface

Under the hood, the PHP engine ensures that all backed enums implement the `BackedEnum` interface, which extends the base `UnitEnum` interface. The engine defines it as follows:

```php theme={"dark"}
interface BackedEnum extends UnitEnum {
    public static function from(int|string $value): static;
    public static function tryFrom(int|string $value): ?static;
}
```

The read-only `$value` property is not declared within the `BackedEnum` interface itself, as PHP interfaces traditionally cannot declare properties. Instead, the PHP engine automatically exposes the `$value` property directly on the instantiated backed enum objects.

Because it extends `UnitEnum`, a backed enum also inherits the `cases()` method, which returns an array of all cases defined in the enumeration in their order of declaration.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
