Table of Contents

Request for Comments: Function autoloading through spl_autoload*

Introduction

The topic of supporting function autoloading was brought up many times in the past, this RFC introduces a potential implementation which would be consistent with what we have for autoloading classes.

Proposal

The suggested change would be pretty straightforward and BC compatible:

Future improvements

Notice that currently only functions are proposed, but we could implement autoloading other tokens (SPL_AUTOLOAD_CONSTANT, etc.) with this interface.

Examples

 <?php
 // old behavior
 spl_autoload_register(
  function ($name) {
   // include the class definition
   /* ... */
  }
 );
 
 // autoload functions
 spl_autoload_register(
  function ($name) {
   // include the function definition
   /* ... */
  },
  true,
  false,
  SPL_AUTOLOAD_FUNCTION
 );
 
 // autoload mixed
 spl_autoload_register(
  function ($name, $type) {
   switch($type){
    case SPL_AUTOLOAD_CLASS:
     /* ... */
     break;
    case SPL_AUTOLOAD_FUNCTION:
     /* ... */
     break;      
   }
  },
  true,
  false,
  SPL_AUTOLOAD_CLASS|SPL_AUTOLOAD_FUNCTION
 );

Patch

Patches welcome! :)

Changelog

* 2011.08.17 - replace the T_* constants with SPL_AUTOLOAD_*