Skip to content

Phalcon autoload

NOTE

All classes are prefixed with Phalcon

Autoload\Exception

Class Source on GitHub

Exceptions thrown in Phalcon\Autoload will use this class

Autoload\Exceptions\LoaderDirectoriesNotArray

Class Source on GitHub

Uses Phalcon\Autoload\Exception

Method Summary

Methods

Public · 1

__construct()

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

Autoload\Exceptions\LoaderMethodNotCallable

Class Source on GitHub

Uses Phalcon\Autoload\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Autoload\Loader

Class Source 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.

Uses Phalcon\Autoload\Exceptions\LoaderDirectoriesNotArray · Phalcon\Autoload\Exceptions\LoaderMethodNotCallable · Phalcon\Events\AbstractEventsAware

Method Summary

public __construct( bool $isDebug = false ) Loader constructor. public static addClass(string $name,string $file) Adds a class to the internal collection for the mapping public static addDirectory( string $directory ) Adds a directory for the loaded files public static addExtension( string $extension ) Adds an extension for the loaded files public static addFile( string $file ) Adds a file to be added to the loader public static addNamespace(string $name,mixed $directories,bool $prepend = false) public bool autoload( string $className ) Autoloads the registered classes public string|null getCheckedPath() Get the path the loader is checking for a path public array getClasses() Returns the class-map currently registered in the autoloader public array getDebug() Returns debug information collected public array getDirectories() Returns the directories currently registered in the autoloader public array getExtensions() Returns the file extensions registered in the loader public array getFiles() Returns the files currently registered in the autoloader public string|null getFoundPath() Get the path when a class was found public array getNamespaces() Returns the namespaces currently registered in the autoloader public bool isRegistered() returns isRegistered public void loadFiles() Checks if a file exists and then adds the file by doing virtual require public static register( bool $prepend = false ) Register the autoload method public static setClasses(array $classes,bool $merge = false) Register classes and their locations public static setDirectories(array $directories,bool $merge = false) Register directories in which "not found" classes could be found public static setExtensions(array $extensions,bool $merge = false) Sets an array of file extensions that the loader must try in each attempt public static setFileCheckingCallback( mixed $method = null ) Sets the file check callback. public static setFiles(array $files,bool $merge = false) Registers files that are "non-classes" hence need a "require". This is public static setNamespaces(array $namespaces,bool $merge = false) Register namespaces and their related directories public static unregister() Unregister the autoload method protected bool requireFile( string $file ) If the file exists, require it and return true; false otherwise

Properties

protected string|null $checkedPath = null
protected array $classes = []
protected array $debug = []
protected array $directories = []
protected array $extensions = []
protected string|callable $fileCheckingCallback = "is_file"
protected array $files = []
protected string|null $foundPath = null
protected bool $isDebug = false
protected bool $isRegistered = false
protected array $namespaces = []
protected int $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