|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-10-06 21:38 UTC] martin at auswoeger dot com
Description:
------------
If a class implements an interface for which the registered autoloader throws an exception, the class is actually defined afterwards but without the interface.
Test script:
---------------
<?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());
Expected result:
----------------
Fatal error: Uncaught Exception in …
thrown in … on line 7
Actual result:
--------------
object(Foo)#3 (0) {
}
bool(false)
array(0) {
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 22:00:01 2025 UTC |
Allowing the creation of class Foo to go ahead even if its interface failed to autoload would also bite users who use type declarations in function signatures. <?php function dowithfoo(Foointerface $foo) { return; } dowithfoo(new Foo()); ?>