add in drupal_function_exist

mfer - October 3, 2008 - 10:01
Project:Autoload
Version:6.x-1.0
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

For the last 2 or 3 days I've been thinking of writing the same module. Thanks for saving me the work.

How about a backport of drupal_function_exists to this module, as well, for function autoloading.

#1

Crell - October 3, 2008 - 15:27

Well, that would still require an explicit registration for every function you're going to use, and since most of Drupal doesn't know about lazy loading at this point I'm not entirely sure of the value there. The need to explicitly load the function also makes that less useful out in module code. I can be convinced of it, however. :-)

#2

mfer - October 3, 2008 - 16:01

I'm not sure how often I'd use drupal_function_exists (even in D7) but this might be a good way to start to know how to use it.

There would have to be a registry, just like for classes and interfaces. I was thinking something along the lines of:

<?php
function example_autoload_info() {
  return array(
   
'class' => array(
     
'ExampleClass' => array(
       
'file' => 'example.classes.inc',
      ),
    ),
   
'interface' => array(
     
'ExampleInterface' => array(
       
'file' => 'example.classes.inc',
      ),
    ),
   
'function' => array(
     
'example_function' => array(
       
'file' => 'example.function.inc',
      ),
    ),
  );
}
?>

Although, both classes and interfaces could be lumped together (which is what I would do). This does require you to register your functions (something you don't have to do in D7) and use drupal_function_exists to load them.

I could see some real benefits in moving to drupal 7. For example, I will regularly have an optional inc file for my functions called from hook_user. I'll include the file and then call the function. This code only fires occasionally so it works and it's not something I can tell to autoload from the menu system.

In drupal 7 I would use drupal_function_exists to load the functions rather than manually specifying to include the file (in my attempt to do things the D7 way). If we have that ability here I would start doing that well before D7 is out.

If it doesn't go in I'm ok with that. Being able to optionally load classes and interfaces is fantastic by itself and I use it all the time.

If it makes a difference I'd even volunteer to write the patch to add drupal_function_exists in. :)

#3

Crell - October 3, 2008 - 17:21

Hm. OK, I can see that, especially as a nice way around the $op parameter that needs to die. :-)

Even though there's been a stable release I think it's early enough that we can change the API a bit as you describe above. I need to do a new roll after fixing #316584: incompatible drupal_alter() call and info in README anyway.

I'd agree with merging classes and interfaces. I'm not entirely sure why I gave them separate autoloaders in D7, frankly. :-)

I'd also suggest having 2 separate caches, one for classes/interfaces and one for functions. That should help reduce the "giant array in memory" issue.

I guess we should also implement per-callback caching like D7 has, too, to reduce the performance impact of having many autoload hits. Dagnabbit, I knew this would happen!

I look forward to your patch. :-)

 
 

Drupal is a registered trademark of Dries Buytaert.