A PHP class Autoloader

Source: Internet
Author: User
Tags autoloader php class

PHP Autoloader:

This was a class for PHP that keeps the user from have to manually include classes by automatically including them on-the -fly as needed.

This simple Autoloader class was easy-to-use and features caching so it does not has the to scan the file system for a given C Lass more than once (unless, the file is moved).

Installation

Simply include the class and configure it in your top-level/application config:

<?php
require(APP_PATH . ‘classes/Autoloader.class.php‘);
Autoloader::setCacheFilePath(APP_PATH . ‘tmp/class_path_cache.txt‘);
Autoloader::excludeFolderNamesMatchingRegex(‘/^CVS|\..*$/‘);
Autoloader::setClassPaths(array(
    APP_PATH . ‘classes/‘,
    APP_PATH . ‘models/‘
));
spl_autoload_register(array(‘Autoloader‘, ‘loadClass‘));

The above example assumes there is an constant that points to the root of the APP_PATH application where you might has a tmp and directory, among other things.

How It Works

It scans all paths recursively with the order in which they were given until the class is found. The class is LoadeD, and the path to the class was saved to the cache file. Next time the class isEd, the process starts -to needed, its path is pulled directly from the cache file . If the class was moved or the cache file is Remov that class.

By default, it looks to files with exact name as the class with the suffix .class.php . For example, if was MyClassName requested, it looks for MyClassName.class.php . The suffix can changed by calling setClassFileSuffix() :

Autoloader::setClassFileSuffix(‘-my_suffix.php‘);

A PHP class Autoloader

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.