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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
41 - 7 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 04:01:28 2024 UTC