PHPackages                             riimu/kit-classloader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. riimu/kit-classloader

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

riimu/kit-classloader
=====================

PSR-0 and PSR-4 compatible class autoloader

v4.4.0(8y ago)41791MITPHPPHP &gt;=5.6.0

Since Mar 11Pushed 8y ago1 watchersCompare

[ Source](https://github.com/Riimu/Kit-ClassLoader)[ Packagist](https://packagist.org/packages/riimu/kit-classloader)[ Docs](http://kit.riimu.net)[ RSS](/packages/riimu-kit-classloader/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (4)Dependencies (3)Versions (11)Used By (1)

PSR-0 and PSR-4 Class Autoloader
================================

[](#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](http://www.php-fig.org/psr/psr-0/) and [PSR-4](http://www.php-fig.org/psr/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:

[![Travis](https://camo.githubusercontent.com/68b5f5daea5d2f85361e4c2a0ed87f9c9aa48a452438680216b8154810412e55/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f5269696d752f4b69742d436c6173734c6f616465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/Riimu/Kit-ClassLoader)[![Scrutinizer](https://camo.githubusercontent.com/c1b7b92424c70ebe6378403af5682b99464733115c90e0d5fc097e2087ecf7c1/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f5269696d752f4b69742d436c6173734c6f616465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/Riimu/Kit-ClassLoader/)[![Scrutinizer Coverage](https://camo.githubusercontent.com/e373fa8e4df00a866ce83d620d827f241285cf5ef4aa16489cd79f2f2d9f79b6/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f5269696d752f4b69742d436c6173734c6f616465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/Riimu/Kit-ClassLoader/)[![Packagist](https://camo.githubusercontent.com/13143dd150373968b520e8878178d19cbf719b148152bed3e60fad6101ad3182/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7269696d752f6b69742d636c6173736c6f616465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/riimu/kit-classloader)

Requirements
------------

[](#requirements)

- The minimum supported PHP version is 5.6

Installation
------------

[](#installation)

### Installation with Composer

[](#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](https://getcomposer.org/download/)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

[](#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

[](#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](https://github.com/Riimu/Kit-ClassLoader/releases/latest)and extracting the `src` folder to your project. You may then include the provided `src/autoload.php` file to load the library classes.

Usage
-----

[](#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)

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:

```
