# RFC: Function autoloading through spl_autoload*

- Status: Inactive
- Authors: Ferenc Kovacs tyra3l@gmail.com
- Date: 2011-08-05
- Version: 1.0
- Original: https://wiki.php.net/rfc/autofunc
- Page: https://externals.io/rfc/autofunc
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Discussions on the mailing list

- [\[RFC\] Function autoloading through spl_autoload*](https://externals.io/message/54425.md) — started on 2011-08-06, 39 messages
- [Autoload Functions RFC in 5.5?](https://externals.io/message/63776.md) — started on 2012-11-08, 1 messages
- [Autoloading functions/consts without a performance impact](https://externals.io/message/107967.md) — started on 2020-01-03, 16 messages

---

## 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:

- Add two new constants to spl: SPL\_AUTOLOAD\_CLASS, SPL\_AUTOLOAD\_FUNCTION.
- Add a fourth optional parameter for spl\_autoload\_register called $types with the default value of SPL\_AUTOLOAD\_CLASS (this would keep the BC), supported values would be any combination of SPL\_AUTOLOAD\_CLASS and SPL\_AUTOLOAD\_FUNCTION for now. 
    - As you would guess, this would work the same way as the $error\_types parameter works for set\_error\_handler: you can specify for which type(s) of missing tokens the autoloader should be called.
- The type for the missing token should also be passed to the $autoload\_function callback as a second param. 
    - This is needed to be able to handle multiple types of tokens with a common callback.
    - Note that passing more parameters to a function than it has in its definition is valid, this would also be a backward compatible change.
- Change the current class autoloading to only call the autoloaders which match with the SPL\_AUTOLOAD\_CLASS types.
- Add the function autoloading to only call the autoloaders which match with the SPL\_AUTOLOAD\_FUNCTION types.

### 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
 <?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! :)

### Related RFCs

- [RFC \_\_autodefine](https://externals.io/rfc/autodefine)

### Related discussions

- <http://www.mail-archive.com/internals@lists.php.net/msg43983.html>
- <http://www.mail-archive.com/internals@lists.php.net/msg48986.html>
- <http://www.mail-archive.com/internals@lists.php.net/msg52287.html>
- <http://www.mail-archive.com/internals@lists.php.net/msg48820.html>

### Changelog

\* 2011.08.17 - replace the T\_\* constants with SPL\_AUTOLOAD\_\*
