PHP Autoloading with namespaces: Load classes mapping namespaces to directories

Recommend this page to a friend!
  Info   View files (4)   Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 612 All time: 5,062 This week: 370
Version License PHP version Categories
php-autoload-ns 1.0GNU General Publi...5.3PHP 5, Language
Description Author

This class can load classes mapping namespaces to directories.

It register a class automatic loader function that loads class files based on the classes' namespace and class name.

The namespace backslash and the class name underscores are mapped to directory separator characters to find the class file path.

The base include directory paths and the class file name prefix are configurable parameters.

Picture of Sebastian Potasiak
  Performance   Level  
Name: Sebastian Potasiak <contact>
Classes: 8 packages by
Country: Poland Poland
Age: ???
All time rank: 65117 in Poland Poland
Week rank: 713 19 in Poland Poland
Innovation award
Innovation award
Nominee: 3x

Loader class

Loader class allows you to create many classes with autoloading function based on namespaces.

__IMPORTANT__: This loader class is based on directory=namespace system, so if you define your class namespace as:

    namespace this\is\my\stuff;

Class should be located in directory:

    [include path]/this/is/my/stuff/


1. Parameters

    * string[] $_includePaths - there are stored all user-defined include paths, as array of string (the best practice is to store absolute paths),

    * string[] $_fileExtensions - there are stored all user-defined class files extensions (like .php, .class.php, etc.),

    * string[string] $_loadedClasses - there are stored all already loaded classes, so they won't be loaded again (by the same loader instance) they are stored as [class name] => [class file path].

2. Methods

    * _Loader_ setIncludePath(_string_ $path[, bool $prepend = true]) - use this to set a new include path. Include path should be path to directory, where class files are located. You can also decide if new path should be appended or prepended (second parameter). Function returns current Loader class instance (for chaining),

    * _string[]_ getIncludePaths() - use this to check out which directories you have already set,

    * _Loader_ setFileExtension(string $ext[, bool $prepend = false]) - use this to set a new class file extension. There's a second parameter, which works just like in _setIncludePath()_ function. Function returns current Loader class instance (for chaining),

    * _string[]_ getFileExtensions() - use this to check out which class files extensions you have already set,

    * _Loader_ load(string $className) - this is autoloading function (executes when some function is called). You can use this also manually, giving name of the class you want as the parameter,

    * _Loader_ register() - registers autoloading function. Autoloading is available AFTER this function is called,

    * _Loader_ unregister - unregisters autoloading function. Disables autoloading AFTER this function is called.
File Role Description
classes (1 file, 1 directory) Doc. Read this before you use this class
example.php Example Example of using loader

  Files  /  classes  
File Role Description
MyPackage (1 file)
   loader.php Class Main loader class

  Files  /  classes  /  MyPackage  
File Role Description
   Test.php Class Class used for example

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:5,062
This week:370
User Comments (1)

For more information send a message to info at phpclasses dot org.