Skip to content

Anonymous class reference in trigger_error / thrown Exception #13097

Closed
@henzeb

Description

@henzeb

Description

The following code is a bug since the introduction of anonymous classes in PHP 7.0:

https://3v4l.org/0OTZ9#v7.0.0 // for reference
https://3v4l.org/0OTZ9#v8.3.1

<?php

$anonymous = new class(){};

echo get_class($anonymous).': now you see me...';

trigger_error(
    get_class($anonymous).' ...now you don\'t!', 
    E_USER_ERROR
);

Note: it doesn't matter if get_class or $anonymous::class is used. As soon as it is an anonymous class name, it fails.

It also fails when throwing an Exception:

<?php

$anonymous = new class(){};

echo get_class($anonymous).': now you see me...';

throw new Exception(
    get_class($anonymous).' ...now you don\'t!', 
    E_USER_ERROR
);

Resulted in this output:

class@anonymous/in/0OTZ9:3$0: now you see me...
Fatal error: class@anonymous in /in/0OTZ9 on line 7

Process exited with code 255.

But I expected this output instead:

class@anonymous/in/0OTZ9:3$0: now you see me...
Fatal error: class@anonymous ... now you don't in /in/0OTZ9 on line 7

Process exited with code 255.

PHP Version

PHP 8.3.1

Operating System

No response

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