---
title: "Phalcon Autoload"
version: "5.20"
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.phalcon.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Phalcon Autoload

:::info[NOTE]
All classes are prefixed with `Phalcon`
:::

## Autoload\Exception

Class

Exceptions thrown in Phalcon\Autoload will use this class

- `\Exception`
- **`Phalcon\Autoload\Exception`**
- [`Phalcon\Autoload\Exceptions\LoaderDirectoriesNotArray`](#autoloadexceptionsloaderdirectoriesnotarray)
- [`Phalcon\Autoload\Exceptions\LoaderMethodNotCallable`](#autoloadexceptionsloadermethodnotcallable)

## Autoload\Exceptions\LoaderDirectoriesNotArray

Class

- `\Exception`
- [`Phalcon\Autoload\Exception`](#autoloadexception)
- **`Phalcon\Autoload\Exceptions\LoaderDirectoriesNotArray`**

`Phalcon\Autoload\Exception`

### Method Summary

<ApiItem href="#autoloadexceptionsloaderdirectoriesnotarray-__construct" visibility="public" name="__construct" returnType="" params={[{"type":"string","name":"name","default":"\"\""}]}>
</ApiItem>

### Methods

<h4 id="autoloadexceptionsloaderdirectoriesnotarray-__construct"><code>__construct()</code></h4>

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

## Autoload\Exceptions\LoaderMethodNotCallable

Class

- `\Exception`
- [`Phalcon\Autoload\Exception`](#autoloadexception)
- **`Phalcon\Autoload\Exceptions\LoaderMethodNotCallable`**

`Phalcon\Autoload\Exception`

### Method Summary

<ApiItem href="#autoloadexceptionsloadermethodnotcallable-__construct" visibility="public" name="__construct" returnType="" params={[]}>
</ApiItem>

### Methods

<h4 id="autoloadexceptionsloadermethodnotcallable-__construct"><code>__construct()</code></h4>

```php
public function __construct();
```

## Autoload\Loader

Class

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`**

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

### Method Summary

<ApiItem href="#autoloadloader-__construct" visibility="public" name="__construct" returnType="" params={[{"type":"bool","name":"isDebug","default":"false"}]}>
Loader constructor.
</ApiItem>
<ApiItem href="#autoloadloader-addclass" visibility="public" name="addClass" returnType="static" params={[{"type":"string","name":"name","default":null},{"type":"string","name":"file","default":null}]}>
Adds a class to the internal collection for the mapping
</ApiItem>
<ApiItem href="#autoloadloader-adddirectory" visibility="public" name="addDirectory" returnType="static" params={[{"type":"string","name":"directory","default":null}]}>
Adds a directory for the loaded files
</ApiItem>
<ApiItem href="#autoloadloader-addextension" visibility="public" name="addExtension" returnType="static" params={[{"type":"string","name":"extension","default":null}]}>
Adds an extension for the loaded files
</ApiItem>
<ApiItem href="#autoloadloader-addfile" visibility="public" name="addFile" returnType="static" params={[{"type":"string","name":"file","default":null}]}>
Adds a file to be added to the loader
</ApiItem>
<ApiItem href="#autoloadloader-addnamespace" visibility="public" name="addNamespace" returnType="static" params={[{"type":"string","name":"name","default":null},{"type":"mixed","name":"directories","default":null},{"type":"bool","name":"prepend","default":"false"}]}>
</ApiItem>
<ApiItem href="#autoloadloader-autoload" visibility="public" name="autoload" returnType="bool" params={[{"type":"string","name":"className","default":null}]}>
Autoloads the registered classes
</ApiItem>
<ApiItem href="#autoloadloader-getcheckedpath" visibility="public" name="getCheckedPath" returnType="string|null" params={[]}>
Get the path the loader is checking for a path
</ApiItem>
<ApiItem href="#autoloadloader-getclasses" visibility="public" name="getClasses" returnType="array" params={[]}>
Returns the class-map currently registered in the autoloader
</ApiItem>
<ApiItem href="#autoloadloader-getdebug" visibility="public" name="getDebug" returnType="array" params={[]}>
Returns debug information collected
</ApiItem>
<ApiItem href="#autoloadloader-getdirectories" visibility="public" name="getDirectories" returnType="array" params={[]}>
Returns the directories currently registered in the autoloader
</ApiItem>
<ApiItem href="#autoloadloader-getextensions" visibility="public" name="getExtensions" returnType="array" params={[]}>
Returns the file extensions registered in the loader
</ApiItem>
<ApiItem href="#autoloadloader-getfiles" visibility="public" name="getFiles" returnType="array" params={[]}>
Returns the files currently registered in the autoloader
</ApiItem>
<ApiItem href="#autoloadloader-getfoundpath" visibility="public" name="getFoundPath" returnType="string|null" params={[]}>
Get the path when a class was found
</ApiItem>
<ApiItem href="#autoloadloader-getnamespaces" visibility="public" name="getNamespaces" returnType="array" params={[]}>
Returns the namespaces currently registered in the autoloader
</ApiItem>
<ApiItem href="#autoloadloader-isregistered" visibility="public" name="isRegistered" returnType="bool" params={[]}>
Returns isRegistered
</ApiItem>
<ApiItem href="#autoloadloader-loadfiles" visibility="public" name="loadFiles" returnType="void" params={[]}>
Checks if a file exists and then adds the file by doing virtual require
</ApiItem>
<ApiItem href="#autoloadloader-register" visibility="public" name="register" returnType="static" params={[{"type":"bool","name":"prepend","default":"false"}]}>
Register the autoload method
</ApiItem>
<ApiItem href="#autoloadloader-setclasses" visibility="public" name="setClasses" returnType="static" params={[{"type":"array","name":"classes","default":null},{"type":"bool","name":"merge","default":"false"}]}>
Register classes and their locations
</ApiItem>
<ApiItem href="#autoloadloader-setdirectories" visibility="public" name="setDirectories" returnType="static" params={[{"type":"array","name":"directories","default":null},{"type":"bool","name":"merge","default":"false"}]}>
Register directories in which "not found" classes could be found
</ApiItem>
<ApiItem href="#autoloadloader-setextensions" visibility="public" name="setExtensions" returnType="static" params={[{"type":"array","name":"extensions","default":null},{"type":"bool","name":"merge","default":"false"}]}>
Sets an array of file extensions that the loader must try in each attempt
</ApiItem>
<ApiItem href="#autoloadloader-setfilecheckingcallback" visibility="public" name="setFileCheckingCallback" returnType="static" params={[{"type":"mixed","name":"method","default":"null"}]}>
Sets the file check callback.
</ApiItem>
<ApiItem href="#autoloadloader-setfiles" visibility="public" name="setFiles" returnType="static" params={[{"type":"array","name":"files","default":null},{"type":"bool","name":"merge","default":"false"}]}>
Registers files that are "non-classes" hence need a "require". This is
</ApiItem>
<ApiItem href="#autoloadloader-setnamespaces" visibility="public" name="setNamespaces" returnType="static" params={[{"type":"array","name":"namespaces","default":null},{"type":"bool","name":"merge","default":"false"}]}>
Register namespaces and their related directories
</ApiItem>
<ApiItem href="#autoloadloader-unregister" visibility="public" name="unregister" returnType="static" params={[]}>
Unregister the autoload method
</ApiItem>
<ApiItem href="#autoloadloader-requirefile" visibility="protected" name="requireFile" returnType="bool" params={[{"type":"string","name":"file","default":null}]}>
If the file exists, require it and return true; false otherwise
</ApiItem>

### Properties

<ApiItem kind="property" visibility="protected" name="checkedPath" type="string|null" default="null">
</ApiItem>
<ApiItem kind="property" visibility="protected" name="classes" type="autoload_strings" default="[]">
</ApiItem>
<ApiItem kind="property" visibility="protected" name="debug" type="array&lt;int, string&gt;" default="[]">
</ApiItem>
<ApiItem kind="property" visibility="protected" name="directories" type="autoload_strings" default="[]">
</ApiItem>
<ApiItem kind="property" visibility="protected" name="extensions" type="autoload_strings" default="[]">
</ApiItem>
<ApiItem kind="property" visibility="protected" name="fileCheckingCallback" type="callable" default="&quot;is_file&quot;">
Always holds a callable. The setter accepts a callable or a callable
string and rejects anything else.
</ApiItem>
<ApiItem kind="property" visibility="protected" name="files" type="autoload_strings" default="[]">
</ApiItem>
<ApiItem kind="property" visibility="protected" name="foundPath" type="string|null" default="null">
</ApiItem>
<ApiItem kind="property" visibility="protected" name="isDebug" type="bool" default="false">
</ApiItem>
<ApiItem kind="property" visibility="protected" name="isRegistered" type="bool" default="false">
</ApiItem>
<ApiItem kind="property" visibility="protected" name="namespaces" type="autoload_namespaces" default="[]">
</ApiItem>
<ApiItem kind="property" visibility="protected" name="nestingLevel" type="int" default="0">
</ApiItem>

### Methods

<h4 id="autoloadloader-__construct"><code>__construct()</code></h4>

```php
public function __construct( bool $isDebug = false );
```

Loader constructor.

<h4 id="autoloadloader-addclass"><code>addClass()</code></h4>

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

Adds a class to the internal collection for the mapping

<h4 id="autoloadloader-adddirectory"><code>addDirectory()</code></h4>

```php
public function addDirectory( string $directory ): static;
```

Adds a directory for the loaded files

<h4 id="autoloadloader-addextension"><code>addExtension()</code></h4>

```php
public function addExtension( string $extension ): static;
```

Adds an extension for the loaded files

<h4 id="autoloadloader-addfile"><code>addFile()</code></h4>

```php
public function addFile( string $file ): static;
```

Adds a file to be added to the loader

<h4 id="autoloadloader-addnamespace"><code>addNamespace()</code></h4>

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

<h4 id="autoloadloader-autoload"><code>autoload()</code></h4>

```php
public function autoload( string $className ): bool;
```

Autoloads the registered classes

<h4 id="autoloadloader-getcheckedpath"><code>getCheckedPath()</code></h4>

```php
public function getCheckedPath(): string|null;
```

Get the path the loader is checking for a path

<h4 id="autoloadloader-getclasses"><code>getClasses()</code></h4>

```php
public function getClasses(): array;
```

Returns the class-map currently registered in the autoloader

<h4 id="autoloadloader-getdebug"><code>getDebug()</code></h4>

```php
public function getDebug(): array;
```

Returns debug information collected

<h4 id="autoloadloader-getdirectories"><code>getDirectories()</code></h4>

```php
public function getDirectories(): array;
```

Returns the directories currently registered in the autoloader

<h4 id="autoloadloader-getextensions"><code>getExtensions()</code></h4>

```php
public function getExtensions(): array;
```

Returns the file extensions registered in the loader

<h4 id="autoloadloader-getfiles"><code>getFiles()</code></h4>

```php
public function getFiles(): array;
```

Returns the files currently registered in the autoloader

<h4 id="autoloadloader-getfoundpath"><code>getFoundPath()</code></h4>

```php
public function getFoundPath(): string|null;
```

Get the path when a class was found

<h4 id="autoloadloader-getnamespaces"><code>getNamespaces()</code></h4>

```php
public function getNamespaces(): array;
```

Returns the namespaces currently registered in the autoloader

<h4 id="autoloadloader-isregistered"><code>isRegistered()</code></h4>

```php
public function isRegistered(): bool;
```

Returns isRegistered

<h4 id="autoloadloader-loadfiles"><code>loadFiles()</code></h4>

```php
public function loadFiles(): void;
```

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

<h4 id="autoloadloader-register"><code>register()</code></h4>

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

Register the autoload method

<h4 id="autoloadloader-setclasses"><code>setClasses()</code></h4>

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

Register classes and their locations

<h4 id="autoloadloader-setdirectories"><code>setDirectories()</code></h4>

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

Register directories in which "not found" classes could be found

<h4 id="autoloadloader-setextensions"><code>setExtensions()</code></h4>

```php
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

<h4 id="autoloadloader-setfilecheckingcallback"><code>setFileCheckingCallback()</code></h4>

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

Sets the file check callback.

```php
// 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);
```

<h4 id="autoloadloader-setfiles"><code>setFiles()</code></h4>

```php
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

<h4 id="autoloadloader-setnamespaces"><code>setNamespaces()</code></h4>

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

Register namespaces and their related directories

<h4 id="autoloadloader-unregister"><code>unregister()</code></h4>

```php
public function unregister(): static;
```

Unregister the autoload method

<h4 id="autoloadloader-requirefile"><code>requireFile()</code></h4>

```php
protected function requireFile( string $file ): bool;
```

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

Source: https://docs.phalcon.io/5.20/api/phalcon_autoload/index.mdx
