Home » Phalcon Class Autoloader

Phalcon Class Autoloader

by Online Tutorials Library

Phalcon Class Autoloader

Loader is a class found under PhalconLoader directory. This class consists of some pre-defined rules according to rules it loads class automatically. It also handles error such as if a class does not exist but it is called in any part of program then special handler is called for handling.

In Loader if a class is added according to its need in program, performance is increased as only specific file is included. This technique is known as lazy initialization.

PhalconLoader have 4 methods to implement auto load classes:

  1. Registering Namespaces
  2. Registering Directories
  3. Registering Classes
  4. Registering Files

Registering Namespaces

This method is invoked when our code is organized using namespace or external libraries.

Syntax:

registerNamespaces() method.

It takes an associative array, where keys are namespace prefix and their values are directories of their class location. Now, when the loader tries to find the class namespace separator is replaced by their directory separator.

Note: Always add trailing slash at end of the paths.

Implementation


Registering Directories

In this method, classes are under the register directories. This process is not performance effective as file stats increases for computing.

Implementation


Registering Classes

This method is used when project is placed in folder convention become ineffective for accessing files using class and path. It is the fastest method of auto loading but not recommended as maintenance cost is high.

Implementation


Registering Files

This method is used for registering files that are non-classes. To register files we uses require. This method is very useful when files only have functions.

Implementation

Next TopicPhalcon Logging

You may also like