php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #79474 get_declared_classes Wrong loading order Inheritance
Submitted: 2020-04-14 11:50 UTC Modified: 2020-04-15 08:50 UTC
From: ahwelp at universo dot univates dot br Assigned: cmb (profile)
Status: Closed Package: Class/Object related
PHP Version: 7.4.4 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ahwelp at universo dot univates dot br
New email:
PHP Version: OS:

 

 [2020-04-14 11:50 UTC] 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
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-14 16:29 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2020-04-14 16:29 UTC] 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 16:57 UTC] nikic@php.net
-Status: Feedback +Status: Not a bug
 [2020-04-14 16:57 UTC] 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 20:04 UTC] 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 20:12 UTC] nikic@php.net
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.
 [2020-04-14 20:44 UTC] ahwelp at universo dot univates dot br
It there any work arround besides read the array backwards, ignoring the last element untill the String do not start with Composer\Autoload...
 [2020-04-15 08:50 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=349660
Log: Fix #79474: get_declared_classes Wrong loading order Inheritance
 [2020-04-15 08:50 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=e4bb74f6edd6695e8cd860a2ce14c30fcc68444b
Log: Fix #79474: get_declared_classes Wrong loading order Inheritance
 [2020-04-15 08:50 UTC] phpdocbot@php.net
-Status: Not a bug +Status: Closed
 [2020-04-15 08:50 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem -Assigned To: +Assigned To: cmb
 [2020-04-15 21:26 UTC] mumumu@php.net
Automatic comment from SVN on behalf of mumumu
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=349662
Log: Fix #79474: get_declared_classes Wrong loading order Inheritance
 [2020-04-15 21:30 UTC] phpdocbot@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=106c73e2bbf6dc6b9abe762a7bab43486550c0bc
Log: Fix #79474: get_declared_classes Wrong loading order Inheritance
 [2020-12-30 11:59 UTC] nikic@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=96db49aae4629a51fe3a59233b8da8ddfd64cf69
Log: Fix #79474: get_declared_classes Wrong loading order Inheritance
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 26 12:01:29 2024 UTC