In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, autoloading is the capability of loading and
linking portions of a program from
mass storage
In computing, mass storage refers to the storage of large amounts of data in a persisting and machine-readable fashion. In general, the term is used as large in relation to contemporaneous hard disk drives, but it has been used large in relati ...
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 speed for ease of coding and speed of initial compilation/linking.
Typical autoload systems intercept
procedure calls to undefined
subroutine
In computer programming, a function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.
Functions ma ...
s. The autoloader searches through a
path
A path is a route for physical travel – see Trail.
Path or PATH may also refer to:
Physical paths of different types
* Bicycle path
* Bridle path, used by people on horseback
* Course (navigation), the intended path of a vehicle
* Desire ...
of directories in the computer's
file system
In computing, file system or filesystem (often abbreviated to fs) is a method and data structure that the operating system uses to control how data is stored and retrieved. Without a file system, data placed in a storage medium would be one lar ...
, to find a file containing
source or
object
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an ai ...
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 includes a primitive path searcher, and
Perl
Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
allows individual
modules to determine how and whether autoloading should occur. The
Unix shell
A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system t ...
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
PHP is a General-purpose programming language, general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementati ...
5, autoload functionality is triggered when referencing an undefined
class. One or more autoload functions—implemented as the
__autoload
magic function
Magic or Magick most commonly refers to:
* Magic (supernatural), beliefs and actions employed to influence supernatural beings and forces
* Ceremonial magic, encompasses a wide variety of rituals of magic
* Magical thinking, the belief that unrel ...
or any function registered to the
SPL autoload stack—is called and given the opportunity to define the class, usually by loading the file it is defined in.
PHP
spl_autoload_register(function ($class) {
$file = 'src/' . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) {
require $file;
}
});
External links
PSR-4Improved Autoloading Standard
Autoloading Classesin PHP
spl_autoload_registerin PHP
Programming constructs