Bug #79474 [Nab]: get_declared_classes Wrong loading order Inheritance

From: Date: Tue, 14 Apr 2020 20:12:10 +0000
Subject: Bug #79474 [Nab]: get_declared_classes Wrong loading order Inheritance
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226579@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79474&edit=1 ID: 79474 Updated by: nikic@php.net Reported by: ahwelp at universo dot univates dot br Summary: get_declared_classes Wrong loading order Inheritance Status: Not a bug Type: Bug Package: Class/Object related Operating System: Linux PHP Version: 7.4.4 Block user comment: N Private report: N New Comment: The order changes as part of variance support, which may introduce cyclic dependencies between classes, in which case "dependencies before dependents" is no longer a well-defined order. Previous Comments: ------------------------------------------------------------------------ [2020-04-14 20:04:18] ahwelp at universo dot univates dot br @requinix I don't think so. The documentation page of php.net do not specify a order besides the user comment. @nikic Taking a look at the source, the commit https://github.com/php/php-src/commit/3d90b770e8d490989294101745d161b8347ae9d4 links to the bug #77631 and add that the order is undefined. Yes, this function don't have a defined order behavior on the documentation, but it feels conceptualy strange to load the child class before it's parent. The combined top-down bottom-up approach allow the classes to be loaded in arbitrary order (unlike C), but with the PSR-4 it not up to the developer to include the files and have some kind of controll over the include order. I could not find why the behavior changed, performance maybe? And like @requinix said, for 15 years users expect this behavior from the function. I don't know where and when the requisites for this function were defined and if it's possible to change the requisits. ------------------------------------------------------------------------ [2020-04-14 16:57:10] nikic@php.net Order of get_declared_classes() is unspecified. PHP 7.4, just like previous PHP versions, returns classes in order of registration. PHP 7.4, unlike previous PHP versions, can register a class before its parents, if that was the order in which the classes were loaded. ------------------------------------------------------------------------ [2020-04-14 16:29:37] requinix@php.net Is there anything in the official documentation that says they're returned in a particular order? Besides a user comment from 15 years ago? ------------------------------------------------------------------------ [2020-04-14 11:50:26] ahwelp at universo dot univates dot br Description: ------------ The function get_declared_classes() should return the loaded classes in the order they were defined. When a PSR-4 package is required and one class is extended on the local code, the child class is registered before the parent. In PHP7.3 the parent is registered before the child. In PHP7.4x the child is registered before the parent Test script: --------------- #composer require jasonlewis/menu dev-master #Small package, good for testing <?php include 'vendor/autoload.php'; use JasonLewis\Menu\HtmlBuilder; class BootstrapBuilder extends HtmlBuilder{} var_dump(print_r(get_declared_classes(), true)); Expected result: ---------------- Execution on PHP 7.3 string(4086) "Array ( [0] => stdClass [1] => Exception [2] => ErrorException [3] => Error ..... [137] => ComposerAutoloaderInitca7fe8d0414cd934e64e09ae5654bee8 [138] => Composer\Autoload\ClassLoader [139] => Composer\Autoload\ComposerStaticInitca7fe8d0414cd934e64e09ae5654bee8 [140] => JasonLewis\Menu\HtmlBuilder [141] => BootstrapBuilder ) Actual result: -------------- Execution on PHP 7.4 string(4146) "Array ( [0] => stdClass [1] => Exception [2] => ErrorException ...... [138] => ZipArchive [139] => BootstrapBuilder [140] => ComposerAutoloaderInitca7fe8d0414cd934e64e09ae5654bee8 [141] => Composer\Autoload\ClassLoader [142] => Composer\Autoload\ComposerStaticInitca7fe8d0414cd934e64e09ae5654bee8 [143] => JasonLewis\Menu\HtmlBuilder ) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79474&edit=1

« previous php.bugs (#226579) next »