Autoload
   HOME

TheInfoList



OR:

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 ana ...
, 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 call 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 may ...
s 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 may ...
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 p ...
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 larg ...
, to find a file containing
source Source may refer to: Research * Historical document * Historical source * Source (intelligence) or sub source, typically a confidential provider of non open-source intelligence * Source (journalism), a person, publication, publishing institute o ...
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, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
allows individual
modules Broadly speaking, modularity is the degree to which a system's components may be separated and recombined, often with the benefit of flexibility and variety in use. The concept of modularity is used primarily to reduce complexity by breaking a sy ...
to determine how and whether autoloading should occur. The
Unix shell A Unix shell is a command-line Interpreter (computing), interpreter or shell (computing), shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting langua ...
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 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 implementation is now produced by The PHP Group ...
5, autoload functionality is triggered when referencing an undefined
class Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differentl ...
. One or more autoload functions—implemented as the __autoload magic function or any function registered to the
SPL SPL may refer to: Association football * Saudi Professional League * Scottish Premier League * SportPesa Premier League, Kenya * Singapore Premier League * RoboCup Standard Platform League, matches between autonomous robots Computing Program ...
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-4
Improved Autoloading Standard
Autoloading Classes
in PHP
spl_autoload_register
in PHP Programming constructs