I am trying to use this for a module I'm developing. I was previously using my own autoloading method. Swapping in code using Autoload module instead of my method works great except when I do a fresh install of my module.
Upon installing my module, I get a "Fatal error: Class not found'. It seems as if the installer loads all the include files in my module's directory. It loads them in alphabetical order. This is a problem when, for example 'class_a.inc' is loaded before 'class_b.inc' and class_a extends class_b. PHP tries to use autoload_class() to load class_b, but is unable to do so. Apparently autoload_flush_caches() has not been called at this point.
I tried calling autoload_flush_caches() in my module's implementation of hook_install(), but that did not work.
Any suggestions?
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | autoload_install.patch | 284 bytes | SimmeLj |
Comments
Comment #1
Crell commentedUnfortunately no. The installer doesn't call init hooks, I think, so the autoload isn't even registered yet. It also has not yet called your autoload hook implementation so it doesn't know about your classes anyway. Not sure what to do here. :-(
Comment #2
avpadernoA Drupal hook function is still a function that can be called from any functions, including any install, or update functions that a module could define.
The bigger problem, as I can see, it's the dependency between two classes, but I would expect the autoload function to be called for both the classes, which are not being found on any already loaded file.
Comment #3
Crell commentedPHP's autoload capability will fire repeatedly as necessary if you have, say, multiple classes that inherit from each other. That is a non-issue.
Comment #4
SimmeLj commentedI've added a .install file with a install hook that calls spl_autoload_register(). Works as it's supposed to!
I'll attach a patch for review
Comment #5
Crell commentedHow will #4 help? The problem is module foo, which uses autoload, having trouble on install. autoload_install() only fires once, when autoload itself is installed.
Comment #6
SimmeLj commentedYes, autoload_install only fires once. But it's that one time you need to make it work.
The problem now is that the only place spl_autoload_register() is run is in autoload_init(), which isn't run on install.
If spl_autoload_register() is not invoked how will PHP know to use the autoload module for autoloading classes?
By making autoload register itself on install as well, other modules can make use of it during the install process.
Comment #7
Hugo Wetterberg commentedWell, all the already installed modules always gets their init method called during the normal bootstrap process, before the install is started. Therefore the problem with autoload not being registered as the autoloader at install-time only surfaces when autoload is installed at the same time as the module that depends on it. Sneaky bug, but it should be fixed by SimmeLj's patch.
Comment #8
mfer commentedhook_install is the wrong hook here. We want this to fire when the module is enabled not installed. There is an ever so slight difference. hook_enable should be used.
Changing the hook and providing a nice comment about the case should fix this issue.
Comment #9
Crell commentedI've added the code above to hook_enable(). Thanks folks.