Skip to content

Commit

Permalink
Fix PHP 7.4 regression, changed behavior of get_declared_classes()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stronk7 committed Sep 29, 2020
1 parent afca5ac commit 1de501b
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@ public static function loadFile($path)

$className = null;
$newClasses = array_reverse(array_diff(get_declared_classes(), $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.
$newClasses = array_reduce(
$newClasses,
function ($remaining, $current) {
return array_diff($remaining, class_parents($current));
},
$newClasses
);
foreach ($newClasses as $name) {
if (isset(self::$loadedFiles[$name]) === false) {
$className = $name;
Expand Down

0 comments on commit 1de501b

Please sign in to comment.