PHP Classes

Enhanced PSR 4 Autoloader: Autoloader that supports PSR 4 with enhancements

Recommend this page to a friend!
  Info   View files Example   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 70%Total: 138 This week: 1All time: 9,231 This week: 560Up
Version License PHP version Categories
psr4enhan 1.0.0GNU General Publi...5.3.0PHP 5, Language
Description 

Author

This class is an autoloader that supports PSR 4 with enhancements.

It can register namespaces and assign them to specific directories according to PSR 4 specification.

It can also register an entire namespace and sub-namespaces to a given directory.

A given namespace may also be looked into multiple directories.

The class can also register specific class names for specific files with names totally different to the class names.

Multiple classes may be loaded from the same file.

Innovation Award
PHP Programming Innovation award winner
August 2015
Winner
PSR 4 is a PHP standard recommendation to autoload classes of given namespaces from specific directories.

This class implements PSR 4 autoloading support.

It also supports additional autoloading possibilities like assigning sub-namespaces to given directories, so the it continues to scan sub-directories to find the actual class files, autoloading classes from a namespace from a multiple directories, autoload specific classes from a given path, and autoload multiple classes from the same path.

Manuel Lemos
Picture of Andrei Alexandru
  Performance   Level  
Name: Andrei Alexandru <contact>
Classes: 4 packages by
Country: Spain Spain
Age: 38
All time rank: 251565 in Spain Spain
Week rank: 416 Up14 in Spain Spain Up
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

Example

<?php

require_once 'Autoloader.php';
// true param for auto-registration in spl_autoload_register() function.
$loader = new Autoloader(true);

// Register a namespace
$loader->registerNamespace('org\\example\\libraries', './org/example/libraries');
new
org\example\libraries\DatabaseLibrary();

// Register a new file that has no namespace
$loader->registerFile('NoNamespaceClass', 'org/NoNamespaceClass.php');
new
NoNamespaceClass();


// Register an entire namespace
$loader->registerNamespace('org', 'orgtests');
// Now we can instantiate any of the test classes in org\... namespace
new org\example\controllers\HomeControllerTest();
new
org\example\libraries\DatabaseLibraryTest();

// We can register more locations or directories for one namespace
$loader->registerNamespace('org', 'org');
// now we can instantiate HomeController
new org\example\controllers\HomeController();

// Register a Class that has a diferent filename
$loader->registerFile('SomeClassName', 'index.php');
// Overwrite the last filename for "SomeClassName"
$loader->registerFile('SomeClassName', 'otherClasses/DiferentFileNameAndClassName.php', true);
new
SomeClassName();

// Register more than one class per filename
$loader->registerFile('AnotherClass', 'otherClasses/DiferentFileNameAndClassName.php', true);
$loader->registerFile('YetAnotherClass', 'otherClasses/DiferentFileNameAndClassName.php', true);
// It will find it even if the file has not the same name as the class (you should NOT do this but...)
new AnotherClass();
new
YetAnotherClass();



  Files folder image Files  
File Role Description
Files folder imageorg (2 files, 1 directory)
Files folder imageorgtests (1 directory)
Files folder imageotherClasses (1 file)
Accessible without login Plain text file Autoloader.php Class Main class
Accessible without login Plain text file index.php Example Examples

  Files folder image Files  /  org  
File Role Description
Files folder imageexample (2 directories)
  Accessible without login Plain text file AbstractController.php Class Examples
  Accessible without login Plain text file NoNamespaceClass.php Class Examples

  Files folder image Files  /  org  /  example  
File Role Description
Files folder imagecontrollers (1 file)
Files folder imagelibraries (1 file)

  Files folder image Files  /  org  /  example  /  controllers  
File Role Description
  Accessible without login Plain text file HomeController.php Class Examples

  Files folder image Files  /  org  /  example  /  libraries  
File Role Description
  Accessible without login Plain text file DatabaseLibrary.php Class Examples

  Files folder image Files  /  orgtests  
File Role Description
Files folder imageexample (2 directories)

  Files folder image Files  /  orgtests  /  example  
File Role Description
Files folder imagecontrollers (1 file)
Files folder imagelibraries (1 file)

  Files folder image Files  /  orgtests  /  example  /  controllers  
File Role Description
  Accessible without login Plain text file HomeControllerTest.php Class Examples

  Files folder image Files  /  orgtests  /  example  /  libraries  
File Role Description
  Accessible without login Plain text file DatabaseLibraryTest.php Class Examples

  Files folder image Files  /  otherClasses  
File Role Description
  Accessible without login Plain text file DiferentFileNameAndClassName.php Example Examples

 Version Control Unique User Downloads Download Rankings  
 0%
Total:138
This week:1
All time:9,231
This week:560Up
 User Ratings  
 
 All time
Utility:100%StarStarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:-
Examples:100%StarStarStarStarStarStar
Tests:-
Videos:-
Overall:70%StarStarStarStar
Rank:297