<?php
spl_autoload_register(function ($class) {
if ('Foo' === $class) {
class Foo implements Foointerface {}
} elseif ('Foointerface' === $class) {
throw new Exception();
}
});
try {
// First call triggers the autoloader
new Foo();
} catch (Exception $e) {}
// For the second call Foo is loaded
var_dump(new Foo());
// Even though it’s missing the interface
var_dump(new Foo() instanceof Foointerface);
var_dump((new ReflectionClass('Foo'))->getInterfaces());
Fatal error: Uncaught Exception in /in/s8ti7:7
Stack trace:
#0 /in/s8ti7(5): {closure}('Foointerface')
#1 /in/s8ti7(17): {closure}('Foo')
#2 {main}
thrown in /in/s8ti7 on line 7
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Fatal error: Uncaught Exception in /in/s8ti7:7
Stack trace:
#0 /in/s8ti7(5): {closure}('Foointerface')
#1 /in/s8ti7(17): {closure}('Foo')
#2 {main}
thrown in /in/s8ti7 on line 7
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Fatal error: Uncaught Exception in /in/s8ti7:7
Stack trace:
#0 [internal function]: {closure}('Foointerface')
#1 /in/s8ti7(5): spl_autoload_call('Foointerface')
#2 [internal function]: {closure}('Foo')
#3 /in/s8ti7(17): spl_autoload_call('Foo')
#4 {main}
thrown in /in/s8ti7 on line 7
Process exited with code 255.
Fatal error: During class fetch: Uncaught Exception in /in/s8ti7:7
Stack trace:
#0 [internal function]: {closure}('Foointerface')
#1 /in/s8ti7(5): spl_autoload_call('Foointerface')
#2 [internal function]: {closure}('Foo')
#3 /in/s8ti7(13): spl_autoload_call('Foo')
#4 {main} in /in/s8ti7 on line 5
Process exited with code 255.