PSR-4 compliant, standalone autoloader, designed as an lightweight alternative to Composer autoloading.
✅ PSR-4 Compatible - Supports namespace-based autoloading.
✅ Singleton Pattern - Ensures only one instance is created.
✅ Secure File Inclusion - Prevents duplicate loads and enforces best practices.
✅ Fluent Interface - Allows method chaining for cleaner code.
✅ Performance Optimized - Uses a class map to cache file paths and reduce redundant file system operations.
✅ Error Logging & Handling - Logs missing classes and invalid files instead of crashing execution.
Simply include the Autoloader.php
file in your project.
project/
├── src/
│ ├── Controllers/
│ │ ├── HomeController.php (namespace: App\Controllers)
│ ├── Models/
│ │ ├── User.php (namespace: App\Models)
│ ├── Customize/
│ │ ├── helpers.php (non-class file)
│ ├── inc/
│ │ ├── actions.php (non-class file)
│ │ ├── functions.php (non-class file)
├── vendor/ (optional)
├── Autoloader.php
├── index.php
At the start of your script (index.php
or bootstrap.php
), initialize the autoloader:
require_once 'autoloader.php';
Register namespaces and associate them with their respective directories.
$autoloader = Brisko\Autoloader::init()
->addNamespace('App\\', 'src')
->addNamespace('Lib\\', 'lib', true); // Higher priority
This will automatically load classes based on their namespaces.
For example, App\Controllers\HomeController
will be mapped to:
/src/Controllers/HomeController.php
You can also manually include files that are not class-based (e.g., helper functions, configs).
$autoloader->addFiles([
"src/Customize/helpers.php",
"src/inc/actions.php",
"src/inc/functions.php",
]);
Now, all these files will be included without duplicate loading.
Suppose you have the following file structure:
project/
├── src/
│ ├── Controllers/
│ │ ├── HomeController.php (namespace: App\Controllers)
<?php
namespace App\Controllers;
class HomeController
{
public function index()
{
echo "Hello from HomeController!";
}
}
Now, you can use it like this in index.php
:
use App\Controllers\HomeController;
$controller = new HomeController();
$controller->index();
✅ No need to manually require the file! The autoloader will handle it.
- Prevents Cloning & Unserialization – Ensures only one instance of the autoloader exists.
- Secure Path Handling – Uses
realpath()
to prevent directory traversal attacks. - Checks
is_readable()
before Loading – Avoids including unreadable or malicious files. - Caches Resolved Paths – Prevents unnecessary file system operations.
- Logs missing classes and files using
error_log()
, avoiding script crashes. - Ensures invalid directories cannot be registered as namespace paths.
- If a required file is missing, an error is logged but the application continues.
Feature | ✅ Status |
---|---|
PSR-4 Compliant | ✅ |
Secure & Reliable | ✅ |
Lightweight (No Composer Needed) | ✅ |
Singleton Pattern (Prevents Multiple Instantiations) | ✅ |
Optimized Performance (Class Caching) | ✅ |