Git Product home page Git Product logo

phpautoloader's Introduction

phpautoloader

By Joe Fallon

Joe's PHP Autoloader is a versatile and easy to use autoloader for PHP 5.3 and greater. It provides the following features:

  • When auto-loading a class, all of the include paths are searched automatically.
  • For maximum legacy support, non-namespaced classes are allowed.
  • Normal PSR-0 namespaced classes are supported.
  • Underscore namespaced (e.g. like the Zend Framework 1 or PEAR) classes are supported.
  • This autoloader is fast. No recursive directory searches are performed.

Requirements

The only requirement is PHP > 5.3.0. This is due to the use of namespaces. Additionally, Composer can be helpful.

Installation

The easiest way to install Joe's Autoloader is with Composer. Create the following composer.json file and run the php composer.phar install command to install it.

{
    "require": {
        "joefallon/phpautoloader": "*"
    }
}

Usage

To use Joe's Autoloader, the following initialization steps are needed:

  • Add the base directories where classes can be found to the include path.
  • Call the Autoloader::registerAutoload() method to load the autoloader.
  • Start using classes in your code.
use JoeFallon\AutoLoader;

// Define the include paths.
define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
define('LIB_PATH',  BASE_PATH . '/lib');
define('VEND_PATH', BASE_PATH . '/vendor');

// Set the application include paths for auto-loading.
set_include_path(get_include_path() . ':' . LIB_PATH . ':' . BASE_PATH);

// Require the Composer autoloader. Composer will handle its own class auto-loading
// using its own autoloader.
require(VEND_PATH . '/autoload.php');

// Initialize Joe's Autoloader. Joe's Autoloader will handle auto-loading any classes
// that are not auto-loaded using Composer's built-in autoloader.
Autoloader::registerAutoLoad();

As long as namespaces are mapped to the folder structure within the directories defined above, then autoloader will have no problems finding and loading the classes.

For example, let's assume we want to load the class Bar that is within the file named Bar.php contained within a folder Foo. Also, let's assume that the class Bar is namespaced \Foo\Bar. This would give a file path of LIB_PATH/Foo/Bar.php. When new Bar(); is executed, the Bar class will be loaded (if is was not already).

Here is a visual depiction of the above example:

Joe's Autoloader Example

phpautoloader's People

Contributors

joefallon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.