# Autoload

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Autoload
> Markdown URL: https://mediated.wiki/source/Autoload.md
> Source: https://en.wikipedia.org/wiki/Autoload
> Source revision: 1317281061
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Loading and linking portions of a program from mass storage automatically when needed}}
{{Other uses|Autoloader (disambiguation)}}
{{No footnotes|date=October 2023}}

In [computer programming](/source/computer_programming), '''autoloading''' is the capability of loading and [linking](/source/Linker_(computing)) portions of a program from [mass storage](/source/mass_storage) automatically when needed, so that the programmer is not required to define or include those portions of the program explicitly.  Many high-level programming languages include autoload capabilities, which sacrifice some [run-time](/source/Run_time_(program_lifecycle_phase)) speed for ease of coding and speed of initial compilation/linking.

Typical autoload systems intercept [procedure call](/source/procedure_call)s to undefined [subroutine](/source/subroutine)s.  The autoloader searches through a [path](/source/Path_(computing)) of directories in the computer's [file system](/source/file_system), to find a file containing [source](/source/source_code) or [object](/source/object_code) code that defines the subroutine.  The autoloader then loads and links the file, and hands control back to the main program so that the subroutine gets executed as if it had already been defined and linked before the call.

Many interactive and high-level languages operate in this way.  For example, [IDL](/source/IDL_(programming_language)) includes a primitive path searcher, and [Perl](/source/Perl) allows individual [modules](/source/perl_module) to determine how and whether autoloading should occur.  The [Unix shell](/source/Unix_shell) may be said to consist almost entirely of an autoloader, as its main job is to search a path of directories to load and execute command files.  In [PHP](/source/PHP) 5, autoload functionality is triggered when referencing an undefined [class](/source/Class_(programming)).  One or more autoload functions—implemented as the <code>__autoload</code> [magic function](/source/magic_function) or any function registered to the [SPL](/source/Standard_PHP_Library) autoload stack—is called and given the opportunity to define the class, usually by loading the file it is defined in.

== PHP ==
<syntaxhighlight lang="php">
spl_autoload_register(function ($class) {
    $file = 'src/' . str_replace('\\', '/', $relative_class) . '.php';
    if (file_exists($file)) {
        require $file;
    }
});
</syntaxhighlight>

== External links ==
* [http://www.php-fig.org/psr/psr-4/ PSR-4] Improved Autoloading Standard
* [http://www.php.net/autoload Autoloading Classes] in PHP
* [http://www.php.net/manual/en/function.spl-autoload-register.php spl_autoload_register] in PHP

Category:Programming constructs

---
Adapted from the Wikipedia article [Autoload](https://en.wikipedia.org/wiki/Autoload) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Autoload?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
