|
| 1 | +--TEST-- |
| 2 | +Function autoloading: loader exceptions propagate, failures are not cached |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | +echo "== direct propagates ==\n"; |
| 6 | +$thrower = function (string $name) { |
| 7 | + throw new RuntimeException("Autoload failed for: $name"); |
| 8 | +}; |
| 9 | +spl_autoload_register_function_loader($thrower); |
| 10 | +try { |
| 11 | + missing_func(); |
| 12 | +} catch (RuntimeException $e) { |
| 13 | + echo $e->getMessage(), "\n"; |
| 14 | +} |
| 15 | +// failed autoloads are not cached, so the loader runs again |
| 16 | +try { |
| 17 | + missing_func(); |
| 18 | +} catch (RuntimeException $e) { |
| 19 | + echo $e->getMessage(), "\n"; |
| 20 | +} |
| 21 | + |
| 22 | +echo "== dynamic propagates ==\n"; |
| 23 | +$f = 'missing_func'; |
| 24 | +try { |
| 25 | + $f(); |
| 26 | +} catch (RuntimeException $e) { |
| 27 | + echo $e->getMessage(), "\n"; |
| 28 | +} |
| 29 | +spl_autoload_unregister_function_loader($thrower); |
| 30 | + |
| 31 | +echo "== callable resolution wrapping ==\n"; |
| 32 | +// is_callable() rethrows the loader's exception directly; the call/closure APIs |
| 33 | +// wrap it in a TypeError but keep it as the previous exception. Either way the |
| 34 | +// loader's exception must not be swallowed. |
| 35 | +$thrower = function (string $name) { |
| 36 | + throw new RuntimeException("loader failed"); |
| 37 | +}; |
| 38 | +spl_autoload_register_function_loader($thrower); |
| 39 | +function check(callable $cb): void { |
| 40 | + try { |
| 41 | + $cb(); |
| 42 | + echo "no exception\n"; |
| 43 | + } catch (\Throwable $e) { |
| 44 | + $origin = $e instanceof RuntimeException ? $e : $e->getPrevious(); |
| 45 | + echo get_class($e), " -> ", get_class($origin), ": ", $origin->getMessage(), "\n"; |
| 46 | + } |
| 47 | +} |
| 48 | +check(fn() => is_callable('boom')); |
| 49 | +check(fn() => call_user_func('boom')); |
| 50 | +check(fn() => Closure::fromCallable('boom')); |
| 51 | +spl_autoload_unregister_function_loader($thrower); |
| 52 | + |
| 53 | +echo "== silent decline retries ==\n"; |
| 54 | +// A name that fails to load is retried; there is no negative cache. |
| 55 | +// A loader that silently declines (no throw, no definition) does not poison the |
| 56 | +// name, so a name unloadable now may become loadable later. (The throwing-loader |
| 57 | +// retry is covered by the "direct propagates" section above.) |
| 58 | +$attempts = 0; |
| 59 | +$enabled = false; |
| 60 | +$loader = function (string $name) use (&$attempts, &$enabled) { |
| 61 | + if ($name !== 'late_func') { |
| 62 | + return; |
| 63 | + } |
| 64 | + $attempts++; |
| 65 | + echo "attempt $attempts\n"; |
| 66 | + if ($enabled) { |
| 67 | + eval('function late_func() { return "loaded on retry"; }'); |
| 68 | + } |
| 69 | + // otherwise decline silently: no exception, no definition |
| 70 | +}; |
| 71 | +spl_autoload_register_function_loader($loader); |
| 72 | +try { |
| 73 | + late_func(); |
| 74 | +} catch (Error $e) { |
| 75 | + echo $e->getMessage(), "\n"; |
| 76 | +} |
| 77 | +try { |
| 78 | + late_func(); |
| 79 | +} catch (Error $e) { |
| 80 | + echo $e->getMessage(), "\n"; |
| 81 | +} |
| 82 | +$enabled = true; |
| 83 | +var_dump(late_func()); |
| 84 | +echo "attempts: $attempts\n"; |
| 85 | +spl_autoload_unregister_function_loader($loader); |
| 86 | +?> |
| 87 | +--EXPECT-- |
| 88 | +== direct propagates == |
| 89 | +Autoload failed for: missing_func |
| 90 | +Autoload failed for: missing_func |
| 91 | +== dynamic propagates == |
| 92 | +Autoload failed for: missing_func |
| 93 | +== callable resolution wrapping == |
| 94 | +RuntimeException -> RuntimeException: loader failed |
| 95 | +TypeError -> RuntimeException: loader failed |
| 96 | +TypeError -> RuntimeException: loader failed |
| 97 | +== silent decline retries == |
| 98 | +attempt 1 |
| 99 | +Call to undefined function late_func() |
| 100 | +attempt 2 |
| 101 | +Call to undefined function late_func() |
| 102 | +attempt 3 |
| 103 | +string(15) "loaded on retry" |
| 104 | +attempts: 3 |
0 commit comments