You are here

README.txt in Autoload 6

Same filename and directory in other branches
  1. 6.2 README.txt
ABOUT

The autoload module is a utility module.  It allows other modules to leverage
PHP 5's class autoloading capabilities in a unified fashion.  It does, naturally,
require PHP 5.1.2 or later.

You do not need this module unless you are developing a module that makes use of
classes or you are installing a module that depends on this one.

Drupal 7's integrated self-learning code registry is far better than this 
module anyway, so this module will never be ported to Drupal 7.  Drupal 6
is the only version that will be supported, but the code conventions used by
this module will make moving to Drupal 7 easier for class-using modules.


API

PHP 5 supports automatic class lazy-loading in user-defined ways.  A developer
can register one or more autoload functions.  When PHP encounters a request for
a class that is not yet loaded, it will then call all registered autoload 
functions to allow them to try and load the library in which that class is 
defined.  It then tries to load the class again.  If the class is now available,
it loads normally.  If not, it generates a fatal error just as it would have
if there had been no autoload functions.  The catch, of course, is making sure
the autoload functions know how to find the requested class.

This module provides two hooks, hook_autoload_info() and hook_autoload_info_alter().  
Both are extremely simple.

hook_autoload_info() is a registry-style hook.  That is, it must return an
associative array, in this case of classes and the file in which they may be 
found.  Example:

/**
 * Implementation of hook_autoload_info().
 */
function example_autoload_info() {
  return array(
    'ExampleClass' => array(
      'file' => 'example.classes.inc',
    ),
    'ExampleInterface' => array(
      'file' => 'example.classes.inc',
    ),
  );
}

The keys of the returned array are the names of classes or interfaces.  Each 
entry has two keys, "file" and "file path", with "file path" being optional.  
They behave exactly like the "file" and "file path" keys behave in the menu
system.  If "file path" is not specified, it defaults to the directory of the
module.  "file" specifies in what file, relative to "file path", the class
is defined.

hook_autoload_info_alter() is a typical alter hook, and is provided primarily
because it's just so easy to provide an alter for a registry-style hook it 
seemed wasteful not to.

That's it!  Once you declare a class and its location (and clear the cache!), 
the autoload module knows about it and will lazy-load the class for you as 
needed.  You do not need to make any other changes to your object-using code.
Simply call:

$example = new ExampleClass();

as you normally would anyway and the rest happens automatically.

The included "autoloadtest" module provides a trivial but working example.  It
does not include an info file in order to keep it from showing up in the modules
list.


OPTIMIZATION

Do not fall into the easy trap of having one class per file!  Lazy-loading classes
is intended to reduce the amount of code that needs to be parsed on every page,
thus improving performance.  However, loading multiple files into memory is slower
than loading a single file into memory.  There is a trade-off that you will need
to optimize for your specific use case.  

In general, classes that will be needed very frequently should remain in the
.module file.  Other classes should be logically grouped into separate include
files so that classes and interfaces that are used together are loaded 
together as well.  That skips the autoloading process as well as the extra disk
hit.

When in doubt, benchmarks are your friend.


CREDITS

This module was developed by Larry "Crell" Garfield

File

README.txt
View source
  1. ABOUT
  2. The autoload module is a utility module. It allows other modules to leverage
  3. PHP 5's class autoloading capabilities in a unified fashion. It does, naturally,
  4. require PHP 5.1.2 or later.
  5. You do not need this module unless you are developing a module that makes use of
  6. classes or you are installing a module that depends on this one.
  7. Drupal 7's integrated self-learning code registry is far better than this
  8. module anyway, so this module will never be ported to Drupal 7. Drupal 6
  9. is the only version that will be supported, but the code conventions used by
  10. this module will make moving to Drupal 7 easier for class-using modules.
  11. API
  12. PHP 5 supports automatic class lazy-loading in user-defined ways. A developer
  13. can register one or more autoload functions. When PHP encounters a request for
  14. a class that is not yet loaded, it will then call all registered autoload
  15. functions to allow them to try and load the library in which that class is
  16. defined. It then tries to load the class again. If the class is now available,
  17. it loads normally. If not, it generates a fatal error just as it would have
  18. if there had been no autoload functions. The catch, of course, is making sure
  19. the autoload functions know how to find the requested class.
  20. This module provides two hooks, hook_autoload_info() and hook_autoload_info_alter().
  21. Both are extremely simple.
  22. hook_autoload_info() is a registry-style hook. That is, it must return an
  23. associative array, in this case of classes and the file in which they may be
  24. found. Example:
  25. /**
  26. * Implementation of hook_autoload_info().
  27. */
  28. function example_autoload_info() {
  29. return array(
  30. 'ExampleClass' => array(
  31. 'file' => 'example.classes.inc',
  32. ),
  33. 'ExampleInterface' => array(
  34. 'file' => 'example.classes.inc',
  35. ),
  36. );
  37. }
  38. The keys of the returned array are the names of classes or interfaces. Each
  39. entry has two keys, "file" and "file path", with "file path" being optional.
  40. They behave exactly like the "file" and "file path" keys behave in the menu
  41. system. If "file path" is not specified, it defaults to the directory of the
  42. module. "file" specifies in what file, relative to "file path", the class
  43. is defined.
  44. hook_autoload_info_alter() is a typical alter hook, and is provided primarily
  45. because it's just so easy to provide an alter for a registry-style hook it
  46. seemed wasteful not to.
  47. That's it! Once you declare a class and its location (and clear the cache!),
  48. the autoload module knows about it and will lazy-load the class for you as
  49. needed. You do not need to make any other changes to your object-using code.
  50. Simply call:
  51. $example = new ExampleClass();
  52. as you normally would anyway and the rest happens automatically.
  53. The included "autoloadtest" module provides a trivial but working example. It
  54. does not include an info file in order to keep it from showing up in the modules
  55. list.
  56. OPTIMIZATION
  57. Do not fall into the easy trap of having one class per file! Lazy-loading classes
  58. is intended to reduce the amount of code that needs to be parsed on every page,
  59. thus improving performance. However, loading multiple files into memory is slower
  60. than loading a single file into memory. There is a trade-off that you will need
  61. to optimize for your specific use case.
  62. In general, classes that will be needed very frequently should remain in the
  63. .module file. Other classes should be logically grouped into separate include
  64. files so that classes and interfaces that are used together are loaded
  65. together as well. That skips the autoloading process as well as the extra disk
  66. hit.
  67. When in doubt, benchmarks are your friend.
  68. CREDITS
  69. This module was developed by Larry "Crell" Garfield