|
| 1 | +--TEST-- |
| 2 | +Autoloading with closures and invocables |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | +$closure = function ($name) { |
| 6 | + echo 'autoload(' . $name . ")\n"; |
| 7 | +}; |
| 8 | + |
| 9 | +class Autoloader { |
| 10 | + public function __construct(private string $dir) {} |
| 11 | + public function __invoke($class) { |
| 12 | + echo ("Autoloader('{$this->dir}') called with $class\n"); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +class WorkingAutoloader { |
| 17 | + public function __invoke($class) { |
| 18 | + echo ("WorkingAutoloader() called with $class\n"); |
| 19 | + eval("class $class { }"); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +$al1 = new Autoloader('d1'); |
| 24 | +$al2 = new WorkingAutoloader('d2'); |
| 25 | + |
| 26 | +autoload_register_class($closure); |
| 27 | +autoload_register_class($al1); |
| 28 | +autoload_register_class($al2); |
| 29 | + |
| 30 | +var_dump(autoload_list_class()); |
| 31 | + |
| 32 | +$x = new TestX; |
| 33 | + |
| 34 | +autoload_unregister_class($closure); |
| 35 | +autoload_unregister_class($al1); |
| 36 | + |
| 37 | +$y = new TestY; |
| 38 | + |
| 39 | +?> |
| 40 | +--EXPECTF-- |
| 41 | +array(3) { |
| 42 | + [0]=> |
| 43 | + object(Closure)#1 (4) { |
| 44 | + ["name"]=> |
| 45 | + string(%d) "{closure:%s:%d}" |
| 46 | + ["file"]=> |
| 47 | + string(%d) "%s" |
| 48 | + ["line"]=> |
| 49 | + int(2) |
| 50 | + ["parameter"]=> |
| 51 | + array(1) { |
| 52 | + ["$name"]=> |
| 53 | + string(10) "<required>" |
| 54 | + } |
| 55 | + } |
| 56 | + [1]=> |
| 57 | + object(Autoloader)#2 (1) { |
| 58 | + ["dir":"Autoloader":private]=> |
| 59 | + string(2) "d1" |
| 60 | + } |
| 61 | + [2]=> |
| 62 | + object(WorkingAutoloader)#3 (0) { |
| 63 | + } |
| 64 | +} |
| 65 | +autoload(TestX) |
| 66 | +Autoloader('d1') called with TestX |
| 67 | +WorkingAutoloader() called with TestX |
| 68 | +WorkingAutoloader() called with TestY |
0 commit comments