php / php/php-src

Support Explicit Backed Enum casting

Open
#22,288 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Category: Engine Feature Status: Requires RFC
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

https://3v4l.org/pfXH4#v8.5.3

<?php

echo '----- Backed integer enum -----'.PHP_EOL;
enum DayOfWeekInt : int
{
    case Monday = 1;
    case Tuesday = 2;
    case Wednesday = 3;
    case Thursday = 4;
    case Friday = 5;
    case Saturday = 6;
    case Sunday = 7;
}
$var = DayOfWeekInt::Monday;

try {
    echo '--- intval($var) --'.PHP_EOL;
    echo intval($var);    
} catch (\Throwable $ex) {
    echo $ex;
}
echo PHP_EOL;

try {
    echo '--- (int) $var ---'.PHP_EOL;
    echo (int) $var;
} catch (\Throwable $ex) {
    echo $ex;
}
echo PHP_EOL;

echo PHP_EOL;
echo '----- Backed string enum -----'.PHP_EOL;
enum DayofWeekString : string
{
    case Monday = 'monday';
    case Tuesday = 'tuesday';
    case Wednesday = 'wednesday';
    case Thursday = 'thursday';
    case Friday = 'friday';
    case Saturday = 'saturday';
    case Sunday = 'sunday';
}
$var = DayofWeekString::Monday;

try {
    echo '--- strval($var) ---'.PHP_EOL;
    echo strval($var);
} catch (\Throwable $ex) {
    echo $ex;
}
echo PHP_EOL;
    
try {
    echo '--- (string) $var ---'.PHP_EOL;
    echo (string) $var;
} catch (\Throwable $ex) {
    echo $ex;
}
echo PHP_EOL;

Resulted in this output:

----- Backed integer enum -----
--- intval($var) --

Warning: Object of class DayOfWeekInt could not be converted to int in /in/pfXH4 on line 18
1
--- (int) $var ---

Warning: Object of class DayOfWeekInt could not be converted to int in /in/pfXH4 on line 26
1

----- Backed string enum -----
--- strval($var) ---
Error: Object of class DayofWeekString could not be converted to string in /in/pfXH4:48
Stack trace:
#0 {main}
--- (string) $var ---
Error: Object of class DayofWeekString could not be converted to string in /in/pfXH4:56
Stack trace:
#0 {main}

But I expected this output instead:

no warnings, no errors and a correct explicit conversion.

----- Backed integer enum -----
--- intval($var) --
1
--- (int) $var ---
1

----- Backed string enum -----
--- strval($var) ---
monday
--- (string) $var ---
monday
PHP Version
PHP 8.5.3
Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided PHP 8.5.3 reproduction from the issue and compare explicit integer and string casts for backed enums. Trace the enum casting behavior in the PHP source, then add coverage for the expected values and absence of warnings or errors. Done means all four conversions produce the backed values shown in the report.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.