Skip to content

theshem/PHP-Autoload-Class

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Autoload Class

This is a PHP autoload class to load classes dynamically.

Currently, there's no support for namespace feature which has been added since PHP 5.3 version, but it would be added in the future.

Table of Contents

Getting Ready!

require 'autoload.php';

/**
 * AutoLoad::getReady()
 * 
 * Creates an instance of AutoLoad class once
 * and returns the instance for later calls
 *
 * @return object
 */
$loader = AutoLoad::getReady(); // Use $loader or AutoLoad::getReady() itself.

/**
 * AutoLoad::fire();
 *
 * Starts AutoLoading
 */
AutoLoad::getReady()->fire();

Example Usage

require 'autoload.php';

// Get ready, Fire!
AutoLoad::getReady()->addPath('./lib')->addExtension('.class.php')->fire();

// Quick mode:
// Add path while the AutoLoad is getting ready
// This would happen once!
AutoLoad::getReady('./lib')->addExtension('.class.php')->fire();

Function Reference

File Paths

Adding Paths

AutoLoad:: addPath($path)
Parameter Type Description
$path string or array Path of the class files

Return Values
(object) Returns instance of AutoLoad class.

AutoLoad::getReady()->addPath('/path/to/first/directory')
                    ->addPath('/path/to/second/one')
                    ->addPath('/path/to/third');
// OR
$paths = array(
    '/path/to/first/directory',
    '/path/to/second/one',
    '/path/to/third'
);
AutoLoad::getReady()->addPath($paths);

Removing Paths

AutoLoad:: removePath($path)
Parameter Type Description
$path string or array Path of the class files

Return Values
(object) Returns instance of AutoLoad class.

AutoLoad::getReady()->removePath('/path/to/first/directory')
                    ->removePath('/path/to/second/one')
                    ->removePath('/path/to/third');
// OR
$paths = array(
    '/path/to/first/directory',
    '/path/to/second/one',
    '/path/to/third'
);
AutoLoad::getReady()->removePath($paths);

Getting All Paths

AutoLoad:: getPath()

Return Values
(array) Returns all the stored paths.

AutoLoad::getReady()->getPath();

File Extensions

Adding Extensions

AutoLoad:: addExtension($extension)
Parameter Type Description
$extension string or array File extensions of class files.

Return Values
(object) Returns instance of AutoLoad class.

AutoLoad::getReady()->addExtension('.inc')
                    ->addExtension('.lib.php')
                    ->addExtension('.class.php');
// OR
$extensions = array('.inc', '.lib.php', '.class.php');
AutoLoad::getReady()->addExtension($extensions);

Removing Extensions

AutoLoad:: removeExtension($extension)
Parameter Type Description
$extension string or array File extensions of class files.

Return Values
(object) Returns instance of AutoLoad class.

AutoLoad::getReady()->removeExtension('.inc')
                    ->removeExtension('.lib.php')
                    ->removeExtension('.class.php');
// OR
$extensions = array('.inc', '.lib.php', '.class.php');
AutoLoad::getReady()->removeExtension($extensions);

Getting All Extensions

AutoLoad:: getExtension()

Return Values
(array) Returns all the stored file extensions.

AutoLoad::getReady()->getExtension();

Loaded Classes

Check Loaded Class

AutoLoad:: isLoaded($className)
Parameter Type Description
$className string The name of the class.

Return Values
(bool) Returns false if the class is not loaded.

AutoLoad::getReady()->isLoaded($className);

Get Loaded Classes

AutoLoad:: getLoaded()

Return Values
(array) Returns an associative array containing all the loaded class files.

AutoLoad::getReady()->getLoaded();

Manage Autoloading

Start Autoloading, Fire!

AutoLoad:: fire()

Return Values
(void)

Stop Autoloading, Halt!

AutoLoad:: halt()

Return Values
(void)

// Stop autoloading, Halt!
AutoLoad::getReady()->halt();

$f = new Foo('Err :('); // Fatal error: Class 'Foo' not found in...

// Start it again, Fire!
AutoLoad::getReady()->fire();

$f = new Foo('It Works :)');

License

Built by @HashemQolami with ♥ and released under the MIT License.
Any suggestions are welcome. create an issue if you have any problems/ideas.

Thanks,
-Hashem Qolami <hashem@qolami.com>

About

A PHP AutoLoad Class for versions 5.1.2+

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages