Skip to content

Riimu/Kit-ClassLoader

Repository files navigation

PSR-0 and PSR-4 Class Autoloader

Classloader is a PHP library for autoloading classes. Class autoloading means that classes are loaded only when they are actually needed instead of having to include each class file on every execution. This reduces the page loading overhead especially on larger websites, as only some of the class files need to be loaded. Usually the classes are also loaded dynamically from files with file names based on the namespace and class name. This also makes it easier to manage a large number of class files.

This library supports two of the current standards for autoloading classes, namely the PSR-0 and PSR-4. The basic idea behind these standards is that class files reside in directories based on their namespace and in files named after the class. The key difference between these two standards is that PSR-4 does not require the entire namespace to be present in the directory hierarchy.

However, since the operation of finding the actual class files tends to be relatively costly, this library also provides basic caching mechanisms that allow caching the class file locations in a PHP file. With caching, the performance difference between autoloading and loading the files manually becomes negligible.

The API documentation, which can be generated using Apigen, can be read online at: https://kit.riimu.net/api/classloader/

Travis Scrutinizer Scrutinizer Coverage Packagist

Requirements

  • The minimum supported PHP version is 5.6

Installation

Installation with Composer

The easiest way to install this library is to use Composer to handle your dependencies. In order to install this library via Composer, simply follow these two steps:

  1. Acquire the composer.phar by running the Composer Command-line installation in your project root.

  2. Once you have run the installation script, you should have the composer.phar file in you project root and you can run the following command:

    php composer.phar require "riimu/kit-classloader:^4.4"
    

After installing this library via Composer, you can load the library by including the vendor/autoload.php file that was generated by Composer during the installation.

Adding the library as a dependency

If you are already familiar with how to use Composer, you may alternatively add the library as a dependency by adding the following composer.json file to your project and running the composer install command:

{
    "require": {
        "riimu/kit-classloader": "^4.4"
    }
}

Manual installation

If you do not wish to use Composer to load the library, you may also download the library manually by downloading the latest release and extracting the src folder to your project. You may then include the provided src/autoload.php file to load the library classes.

Usage

The ClassLoader supports autoloading as defined by the PSR-0 and PSR-4 standards via the methods addBasePath() and addPrefixPath() respectively. You do not need to understand these standards to use this library, simply use the method that works best for you.

PSR-0 class autoloading

PSR-0 class autoloading defines that class files must be placed in a directory tree that reflects their namespace. For example, the class 'Foo\Bar\Baz' could be located in a file '/path/to/classes/Foo/Bar/Baz.php'. This method is usually the simplest way to place your class files.

Using the addBasePath() method, you can define the base directories where to look for classes. To load the class mentioned above, you could use the following code:

<?php

require 'vendor/autoload.php';
$loader = new Riimu\Kit\ClassLoader\ClassLoader();
$loader->addBasePath('/path/to/classes/');
$loader->register();

$obj = new Foo\Bar\Baz();

If a specific directory only applies to a specific namespace, you can use the second parameter to define the namespace as well. However, The directory still needs to point to the base dir