Skip to content

Missing trait is E_ERROR but missing extends/implements is Exception when autoloaded? #21106

@kkmuffme

Description

@kkmuffme

Description

The following code:

<?php
spl_autoload_register( function ( $class_name ) { 
    if ( $class_name === 'Foo' ) {
        require __DIR__ . '/foo.php';
    }
} );
try {
    class_exists( Foo::class );
} catch ( Throwable $e ) {
    echo 'catch' . PHP_EOL;
}

foo.php

class Foo {
    use Bar;
}

Resulted in this output:

Trait "Bar" not found

But I expected this output instead:

catch

Change foo.php to

class Foo extends Bar {}

and you will get the expected output. It's not consistent/illogical that traits behave differently in this regard?

PHP Version

PHP 8.5

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