php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62808 Using derived autoloader classes doesn't work
Submitted: 2012-08-13 09:23 UTC Modified: 2016-07-03 04:22 UTC
From: michaelduff2 at yahoo dot com Assigned: cmb (profile)
Status: No Feedback Package: SPL related
PHP Version: 5.4.5 OS: RedHat
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: michaelduff2 at yahoo dot com
New email:
PHP Version: OS:

 

 [2012-08-13 09:23 UTC] michaelduff2 at yahoo dot com
Description:
------------
This case breaks autoloading, because of an apparent assumption in the way spl_autoload processes its registered functions:

namespace BaseLibrary
{
    class Loader
    {
         protected static $ns=__NAMESPACE__;
         protected static $dir=__DIR__;

         function Load($class_name)
         {
             // ...fully tested code for autoloading classes
             // using static::$ns and static:$dir...
         }
    }

    class SomeClass
    {
    }
}

namespace DerivedLibrary
{
    class Loader extends \BaseLibrary\Loader
    {
        protected static $ns=__NAMESPACE__;
        protected static $dir=__DIR__;
    }

    class SomeClass extends \BaseLibrary\SomeClass
    {
    }
}

When an attempt is made to load \DerivedLibrary\SomeClass, spl_autoload finds it correctly, include()s the file, but then never calls \BaseLibrary\Loader::Load('\BaseLibrary\SomeClass') -- which is necessary because \DerivedLibrary\SomeClass extends \BaseLibrary\SomeClass and \DerivedLibrary\Loader extends \BaseLibrary\Loader.

It simply throws "Class does not exist."

My guess is that the spl_autoload queue uses globally scoped data rather than function-scoped (stack) data to manage its traversal of the autoload functions, OR because spl_autoload peeks at the function call stack to ascertain which autoloaders it has already called (probably to avoid loops).

If \DerivedLibrary\Loader is made into a verbatim copy of \BaseLibrary\Loader (i.e., copying the file and changing only the namespace declaration -- thereby implicitly removing the 'extends \BaseLibrary\Loader') ... then immediately everything works!


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-24 05:13 UTC] michaelduff2 at yahoo dot com
Any work on this?

Re-reading the test case presented, I see that it is incomplete and slightly misleading (I don't believe \DerivedLibrary\SomeClass needs to extend \BaseLibrary\SomeClass to exhibit the problem), and I also see that no erroneous implementation is provided, nor are the expected vs. actual results.

...So.  I am going to whip up a real test case myself, give it a try on the most recent PHP I have at the moment, and report back here with the results and an executable script.

--Unless someone knows this has been fixed already?
 [2016-06-20 14:43 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2016-06-20 14:43 UTC] cmb@php.net
Yes, please provide a self-contained test script. Note that it's
probably not necessary to include the actual loading code;
echo'ing the relevant variables should be sufficient.
 [2016-07-03 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC