Skip to content

Class Phalcon\Loader

Source on GitHub

| Namespace | Phalcon | | Uses | Phalcon\Loader\Exception, Phalcon\Events\ManagerInterface, Phalcon\Events\EventsAwareInterface | | Implements | EventsAwareInterface |

This component helps to load your project classes automatically based on some conventions

use Phalcon\Loader;

// Creates the autoloader
$loader = new Loader();

// Register some namespaces
$loader->registerNamespaces(
    [
        "Example\\Base"    => "vendor/example/base/",
        "Example\\Adapter" => "vendor/example/adapter/",
        "Example"          => "vendor/example/",
    ]
);

// Register autoloader
$loader->register();

// Requiring this class will automatically include file vendor/example/adapter/Some.php
$adapter = new \Example\Adapter\Some();

Properties

//
protected checkedPath;

/**
 * @var array
 */
protected classes;

/**
 * @var array
 */
protected directories;

//
protected eventsManager;

/**
 * @var array
 */
protected extensions;

//
protected fileCheckingCallback = is_file;

/**
 * @var array
 */
protected files;

/**
 * @var bool
 */
protected foundPath;

/**
 * @var array
 */
protected namespaces;

/**
 * @var bool
 */
protected registered = false;

Methods

public function autoLoad( string $className ): bool;
Autoloads the registered classes

public function getCheckedPath(): string;
Get the path the loader is checking for a path

public function getClasses(): array;
Returns the class-map currently registered in the autoloader

public function getDirs(): array;
Returns the directories currently registered in the autoloader

public function getEventsManager(): ManagerInterface;
Returns the internal event manager

public function getExtensions(): array;
Returns the file extensions registered in the loader

public function getFiles(): array;
Returns the files currently registered in the autoloader

public function getFoundPath(): string;
Get the path when a class was found

public function getNamespaces(): array;
Returns the namespaces currently registered in the autoloader

public function loadFiles(): void;
Checks if a file exists and then adds the file by doing virtual require

public function register( bool $prepend = bool ): Loader;
Register the autoload method

public function registerClasses( array $classes, bool $merge = bool ): Loader;
Register classes and their locations

public function registerDirs( array $directories, bool $merge = bool ): Loader;
Register directories in which "not found" classes could be found

public function registerFiles( array $files, bool $merge = bool ): Loader;
Registers files that are "non-classes" hence need a "require". This is very useful for including files that only have functions

public function registerNamespaces( array $namespaces, bool $merge = bool ): Loader;
Register namespaces and their related directories

public function setEventsManager( ManagerInterface $eventsManager ): void;
Sets the events manager

public function setExtensions( array $extensions ): Loader;
Sets an array of file extensions that the loader must try in each attempt to locate the file

public function setFileCheckingCallback( mixed $callback = null ): Loader;
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);

public function unregister(): Loader;
Unregister the autoload method

protected function prepareNamespace( array $namespaceName ): array;

Class Phalcon\Loader\Exception

Source on GitHub

| Namespace | Phalcon\Loader | | Extends | \Phalcon\Exception |

Phalcon\Loader\Exception

Exceptions thrown in Phalcon\Loader will use this class