Project:Autoload
Version:6.x-1.x-dev
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

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?

Comments

#1

Unfortunately 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. :-(

#2

Assigned to:jtsnow» Anonymous

The installer doesn't call init hooks

A 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.

#3

PHP's autoload capability will fire repeatedly as necessary if you have, say, multiple classes that inherit from each other. That is a non-issue.

#4

Status:active» needs review

I'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

AttachmentSize
autoload_install.patch 284 bytes

#5

How 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.

#6

Yes, 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.

#7

Well, 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.

#8

Status:needs review» needs work

hook_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.

#9

Version:6.x-1.2» 6.x-1.x-dev
Status:needs work» fixed

I've added the code above to hook_enable(). Thanks folks.

#10

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.