Skip to content

Enums can not be used in single file before definition, but just afterwards #19729

@ThomasNunninger

Description

@ThomasNunninger

Description

The following code (https://3v4l.org/troMY):

<?php

/* this works */
enum DefinedInTheBeginning
{
    case FirstCase;
}

$e1 = DefinedInTheBeginning::FirstCase;
print_r($e1);

/* this breaks */

$e2 = DefinedAfterwards::MyCase;
print_r($e2);

enum DefinedAfterwards
{
    case MyCase;
}

Resulted in this output:

DefinedInTheBeginning Enum
(
    [name] => FirstCase
)

Fatal error: Uncaught Error: Class "DefinedAfterwards" not found in /in/troMY:12
Stack trace:
#0 {main}
  thrown in /in/troMY on line 12

Process exited with code 255.

But I expected this output instead:

DefinedInTheBeginning Enum
(
    [name] => FirstCase
)
DefinedAfterwards Enum
{
    [name] => MyCase
}

PHP Version

PHP 8.3.25 (cli) (built: Aug 29 2025 12:01:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.25, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.25, Copyright (c), by Zend Technologies

And https://3v4l.org/troMY

Operating System

Ubuntu 22.04

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions