jetfirephp / autoload
JetFire - Autoload support PSR-4 convention
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-11-09 19:37:33 UTC
README
A PSR-4 autoloader.
Installation
Via composer
composer require jetfirephp/autoload
Usage
To use the autoloader, first instantiate it, then register it with SPL autoloader stack:
require_once __DIR__ . '/vendor/autoload.php'; $loader = new \JetFire\Autoloader\Autoload(); $loader->register();
Add Namespace
To add a single namespace here how to do this :
$loader->addNamespace('App\Admin','/path/to/app-admin/src'); $loader->addNamespace('App\Admin','/path/to/app-admin/tests');
For multiple namespaces :
$loader->setNamespaces([ 'App\Admin' => [ '/path/to/app-admin/src', '/path/to/app-admin/tests' ], 'App\Public' => '/path/to/app-public' ]); // or $loader->setNamespaces('/path/to/namespaces/file.php');
This will override all previous namespace settings.
Add Class
To map a class to a file, use the addClass()
method.
$loader->addClass('App/Admin/Auth','/path/to/app-admin/Auth.php');
For multiple classes :
$loader->setClassCollection([ 'App\Admin\Auth' => '/path/to/app-admin/Auth.php' ]); // or $loader->setClassCollection('path/to/classes/file.php');
Smart Autoloader
If you set a default base directory for your autoloader like this :
$loader = new \JetFire\Autoloader\Autoload('/path/to/base/directory');
And you want to instantiate a class Auth.php
with the namespace App\Admin
when you have not define this namespace in addClass()
method, JetFire\Autoloader will check if the file Auth.php
exists in /path/to/base/directory/App/Admin/
folder.
License
The JetFire Autoloader is released under the MIT public license : http://www.opensource.org/licenses/MIT.