Skip to content

Phalcon Autoload

Updated View as Markdown

Autoload\Exception

ClassSource on GitHub

Exceptions thrown in Phalcon\Autoload will use this class

Autoload\Exceptions\LoaderDirectoriesNotArray

ClassSource on GitHub

Uses Phalcon\Autoload\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $name = "" );

Autoload\Exceptions\LoaderMethodNotCallable

ClassSource on GitHub

Uses Phalcon\Autoload\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Autoload\Loader

ClassSource on GitHub

The Phalcon Autoloader provides an easy way to automatically load classes (namespaced or not) as well as files. It also features extension loading, allowing the user to autoload files with different extensions than .php.

  • Phalcon\Autoload\Loader

Uses Phalcon\Autoload\Exceptions\LoaderDirectoriesNotArray · Phalcon\Autoload\Exceptions\LoaderMethodNotCallable · Phalcon\Contracts\Autoload\AutoloadTypes · Phalcon\Events\Exception · Phalcon\Events\ManagerInterface · Phalcon\Events\Traits\EventsAwareTrait

Method Summary

public__construct( bool$isDebug = false )Loader constructor.publicstaticaddClass(string$name,string$file)Adds a class to the internal collection for the mappingpublicstaticaddDirectory( string$directory )Adds a directory for the loaded filespublicstaticaddExtension( string$extension )Adds an extension for the loaded filespublicstaticaddFile( string$file )Adds a file to be added to the loaderpublicstaticaddNamespace(string$name,mixed$directories,bool$prepend = false)publicboolautoload( string$className )Autoloads the registered classespublicstring|nullgetCheckedPath()Get the path the loader is checking for a pathpublicarraygetClasses()Returns the class-map currently registered in the autoloaderpublicarraygetDebug()Returns debug information collectedpublicarraygetDirectories()Returns the directories currently registered in the autoloaderpublicarraygetExtensions()Returns the file extensions registered in the loaderpublicarraygetFiles()Returns the files currently registered in the autoloaderpublicstring|nullgetFoundPath()Get the path when a class was foundpublicarraygetNamespaces()Returns the namespaces currently registered in the autoloaderpublicboolisRegistered()Returns isRegisteredpublicvoidloadFiles()Checks if a file exists and then adds the file by doing virtual requirepublicstaticregister( bool$prepend = false )Register the autoload methodpublicstaticsetClasses(array$classes,bool$merge = false)Register classes and their locationspublicstaticsetDirectories(array$directories,bool$merge = false)Register directories in which "not found" classes could be foundpublicstaticsetExtensions(array$extensions,bool$merge = false)Sets an array of file extensions that the loader must try in each attemptpublicstaticsetFileCheckingCallback( mixed$method = null )Sets the file check callback.publicstaticsetFiles(array$files,bool$merge = false)Registers files that are "non-classes" hence need a "require". This ispublicstaticsetNamespaces(array$namespaces,bool$merge = false)Register namespaces and their related directoriespublicstaticunregister()Unregister the autoload methodprotectedboolrequireFile( string$file )If the file exists, require it and return true; false otherwise

Properties

protectedstring|null$checkedPath = null
protectedautoload_strings$classes = []
protectedarray<int, string>$debug = []
protectedautoload_strings$directories = []
protectedautoload_strings$extensions = []
protectedcallable$fileCheckingCallback = "is_file"Always holds a callable. The setter accepts a callable or a callable string and rejects anything else.
protectedautoload_strings$files = []
protectedstring|null$foundPath = null
protectedbool$isDebug = false
protectedbool$isRegistered = false
protectedautoload_namespaces$namespaces = []
protectedint$nestingLevel = 0

Methods

Public · 25

__construct()

public function __construct( bool $isDebug = false );

Loader constructor.

addClass()

public function addClass(
    string $name,
    string $file
): static;

Adds a class to the internal collection for the mapping

addDirectory()

public function addDirectory( string $directory ): static;

Adds a directory for the loaded files

addExtension()

public function addExtension( string $extension ): static;

Adds an extension for the loaded files

addFile()

public function addFile( string $file ): static;

Adds a file to be added to the loader

addNamespace()

public function addNamespace(
    string $name,
    mixed $directories,
    bool $prepend = false
): static;

autoload()

public function autoload( string $className ): bool;

Autoloads the registered classes

getCheckedPath()

public function getCheckedPath(): string|null;

Get the path the loader is checking for a path

getClasses()

public function getClasses(): array;

Returns the class-map currently registered in the autoloader

getDebug()

public function getDebug(): array;

Returns debug information collected

getDirectories()

public function getDirectories(): array;

Returns the directories currently registered in the autoloader

getExtensions()

public function getExtensions(): array;

Returns the file extensions registered in the loader

getFiles()

public function getFiles(): array;

Returns the files currently registered in the autoloader

getFoundPath()

public function getFoundPath(): string|null;

Get the path when a class was found

getNamespaces()

public function getNamespaces(): array;

Returns the namespaces currently registered in the autoloader

isRegistered()

public function isRegistered(): bool;

Returns isRegistered

loadFiles()

public function loadFiles(): void;

Checks if a file exists and then adds the file by doing virtual require

register()

public function register( bool $prepend = false ): static;

Register the autoload method

setClasses()

public function setClasses(
    array $classes,
    bool $merge = false
): static;

Register classes and their locations

setDirectories()

public function setDirectories(
    array $directories,
    bool $merge = false
): static;

Register directories in which “not found” classes could be found

setExtensions()

public function setExtensions(
    array $extensions,
    bool $merge = false
): static;

Sets an array of file extensions that the loader must try in each attempt to locate the file

setFileCheckingCallback()

public function setFileCheckingCallback( mixed $method = null ): static;

Sets the file check callback.

// Default behavior.
$loader->setFileCheckingCallback("is_file");

// Faster than `is_file()`, but implies some issues if
// the file is removed from the filesystem.
$loader->setFileCheckingCallback("stream_resolve_include_path");

// Do not check file existence.
$loader->setFileCheckingCallback(null);

setFiles()

public function setFiles(
    array $files,
    bool $merge = false
): static;

Registers files that are “non-classes” hence need a “require”. This is very useful for including files that only have functions

setNamespaces()

public function setNamespaces(
    array $namespaces,
    bool $merge = false
): static;

Register namespaces and their related directories

unregister()

public function unregister(): static;

Unregister the autoload method

Protected · 1

requireFile()

protected function requireFile( string $file ): bool;

If the file exists, require it and return true; false otherwise

Type to search…

↑↓ navigate↵ selectEsc close