Skip to content

Commit 1de501b

Browse files
committed
Fix PHP 7.4 regression, changed behavior of get_declared_classes()
Since PHP 7.4 get_declared_classes() does not guarantee any order. That implies that parent classes aren't the first any more, rendering the array_reverse() technique futile for the loop & break code that follows. So, additionally, let's try to reduce the list of candidates by removing all the classes known to be "parents". That way, at the end, only the "main" class just included with remain. Source: https://raw.githubusercontent.com/php/php-src/PHP-7.4/UPGRADING Text: Previously get_declared_classes() always returned parent classes before child classes. This is no longer the case. No particular order is guaranteed for the get_declared_classes() return value.
1 parent afca5ac commit 1de501b

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

autoload.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ public static function loadFile($path)
168168

169169
$className = null;
170170
$newClasses = array_reverse(array_diff(get_declared_classes(), $classes));
171+
// Since PHP 7.4 get_declared_classes() does not guarantee any order. That
172+
// implies that parent classes aren't the first any more, rendering the
173+
// array_reverse() technique futile for the loop & break code that follows.
174+
// So, additionally, let's try to reduce the list of candidates by removing all
175+
// the classes known to be "parents". That way, at the end, only the "main"
176+
// class just included with remain.
177+
$newClasses = array_reduce(
178+
$newClasses,
179+
function ($remaining, $current) {
180+
return array_diff($remaining, class_parents($current));
181+
},
182+
$newClasses
183+
);
171184
foreach ($newClasses as $name) {
172185
if (isset(self::$loadedFiles[$name]) === false) {
173186
$className = $name;

0 commit comments

Comments
 (0)