
<?php
class autoloader {
public static $loader;
public static function init() {
if (self::$loader == null)
self::$loader = new self ();
return self::$loader;
}
public function __construct() {
spl_autoload_register ( array ($this, 'model' ) );
spl_autoload_register ( array ($this, 'helper' ) );
spl_autoload_register ( array ($this, 'controller' ) );
spl_autoload_register ( array ($this, 'library' ) );
public function library($class) {
set_include_path ( get_include_path () . path_separator . '/lib/' );
spl_autoload_extensions ( '.library.php' );
spl_autoload ( $class );
public function controller($class) {
$class = preg_replace ( '/_controller$/ui', '', $class );
set_include_path ( get_include_path () . path_separator . '/controller/' );
spl_autoload_extensions ( '.controller.php' );
public function model($class) {
$class = preg_replace ( '/_model$/ui', '', $class );
set_include_path ( get_include_path () . path_separator . '/model/' );
spl_autoload_extensions ( '.model.php' );
public function helper($class) {
$class = preg_replace ( '/_helper$/ui', '', $class );
set_include_path ( get_include_path () . path_separator . '/helper/' );
spl_autoload_extensions ( '.helper.php' );
}
//call
autoloader::init ();
?>
1, 在程式使用未聲明的類時會自動調用 __autolaod() 函數來加載;

function __autoload($class_name) {
@require $class_name . '.php';
?>
2.其中 spl_autoload_register() 用來注冊一個自動調用的函數, 可以注冊多個函數!
3.$inipath = ini_get('include_path');ini_set('include_path', $inipath. . $cpath);通過設定環境變量來達到autoload目的,設定包含路徑,以後可以直接包含這些目錄中的檔案,不需要再寫詳細的路徑了。方法三取自php.mvc,使用參照php.mvc文檔

/*
* $header: /phpmvc/phpmvc-base/web-inf/classes/phpmvc/utils/classpath.php,v 1.4 2006/02/22 07:18:26 who exp $
* $revision: 1.4 $
* $date: 2006/02/22 07:18:26 $
*/
class classpath {
// ----- depreciated ---------------------------------------------------- //
/**
* <p>setup the application class paths (php 'include_path') for the included
* class files, for the duration of the main script</p>
*
*<p>returns the class path string for testing purposes
* @depreciated
* @param string the appserverrootdir. eg: 'c:/www/phpmvc'
* @param array an array of sub-application paths,<br>
* eg: $subapppaths[] = 'web-inf/classes/example';, ...
* @param string the os [optional] [unix|windows|mac|...] if we have
* trouble detecting the server os type. eg: path errors.
* @public
* @returns string
*/
function setclasspath($appserverrootdir='', $subapppaths='', $ostype='') {
// set appserver root manually for now
if($appserverrootdir == '') {
echo 'error: classpath :- no php.mvc application root directory specified';
exit;
}
#$_env; // php superglobals !!
// setup the main phpmvc application include() directories here
// note: could be placed in a n xml config file later !!
$appdirs = array();
$appdirs[] = ''; // application root directory
$appdirs[] = 'lib';
// add the sub-application paths, if any
if(is_array($subapppaths)) {
$appdirs = array_merge($appdirs, $subapppaths);
// setup the platform specific path delimiter character
$delim = null; // path delimiter character. (windows, unix, mac!!)
$windir = null;
if( (int)phpversion() > 4 ) {
// php 5
$windir = $_env["windir"]; // see: php v.4.1.0 superglobals
} else {
// php 4
global $http_env_vars; // depreciated-
if( array_key_exists("windir", $http_env_vars) ) {
$windir = $http_env_vars["windir"]; // will be replaced with $_env
}
if($ostype != '') {
if( eregi("windows", $ostype) ) {
$delim = ';'; // windows
} elseif( eregi("unix", $ostype) ) {
$delim = ':'; // unix
} elseif( eregi("mac", $ostype) ) {
$delim = ':'; // mac !!!!!
if($delim == null) {
if( eregi("win", $windir) ) { // _env["c:\\win2k"]
} else {
$delim = ':'; // unix, mac !!
// get the current working directory
$path = $appserverrootdir;
// strip path directories below 'web-inf'
$pathtowebinf = ereg_replace("web-inf.*$", '', $path);
// replace path backslashes with forward slashes
// note: php regular expressions do not work with backslashes
$pathtowebinf = str_replace("\\", "/", $pathtowebinf);
// drop the trailing slash, if one is present
$pathtowebinf = ereg_replace("/$", '', $pathtowebinf);
// setup the environment path string
$classpath = null;
foreach($appdirs as $appdir) {
$classpath .= $pathtowebinf.'/'.$appdir.$delim;
// remove trailing delimiter character
$classpath = substr($classpath, 0, -1);
// setup the include_path for the duration of the main php.mvc script
ini_set('include_path', $classpath);
return $classpath; // for testing
// ----- public methods ------------------------------------------------- //
function getclasspath($appserverrootdir='', $appdirs, $ostype='') {
if($ostype == '') {
// php's build in constant "path_separator" [unix (:) / win (;)]
$delim = path_separator;
// it is handy to be able to specift the os type for testing
$delim = classpath::getpathdelimiter($ostype);
$classpath = null;
$absolutepath = false; // say: "/some/unix/path/" or "d:\some\win\path"
// check if the specified system path is an absolute path. absolute system
// paths start with a "/" on unix, and "ch\:" or "ch/:" on win 32.
// eg: "/some/unix/path/" or "d:\some\win\path" or "d:/some/win/path".
$absolutepath = classpath::absolutepath($appdir);
if($absolutepath == true) {
$classpath .= $appdir.$delim;
$classpath .= $pathtowebinf.'/'.$appdir.$delim;
* concatenate environment path strings
* <p>
* returns the two path strings joined with the correct environment
* string delimiter for the host operating system.
*
* @param string the path string
* @param string the operating type [optional]
* @returns string
function concatpaths($path1, $path2, $ostype='') {
$delim = classpath::getpathdelimiter($ostype);
$path = $path1 . $delim . $path2;
return $path;
// ----- protected methods ---------------------------------------------- //
* get environment path delimiter.
* returns the environment string delimiter for the host operating system.
* @protected
function getpathdelimiter($ostype='') {
return $delim;
/**
* check if the specified system path is an absolute path. absolute system
* paths start with a "/" on unix, and "ch\:" or "ch/:" on win 32.
* eg: "/some/unix/path/" or "d:\some\win\path" or "d:/some/win/path".
* returns true if the suppplied path absolute, otherwise returns false
* @param string the path to check, like: "/some/unix/path/" or
* "d:\some\win\path".
* @returns boolean
function absolutepath($systempath) {
// say: "/some/unix/path/" or "d:\some\win\path" or "d:/some/win/path"
$fabsolutepath = false; // boolean flag value
//"[/]some/unix/path/"
if (preg_match("/^\//", $systempath)) {
$fabsolutepath = true;
//"[d:\]some\win\path"
// "i" says "ignore case"
// note the extra escape "\" reqd for this to work with php !!!
} elseif(preg_match("/^[a-z]:\\\/i", $systempath)) {
//"[d:/]some/win/path"
} elseif(preg_match("/^[a-z]:\//i", $systempath)) {
return $fabsolutepath;

* $header: oohforms/web-inf/modulepaths.php
* $revision:
* $date: 2003.04.22
*
* ====================================================================
* the module paths
* @author john c wildenauer
* @version
* @public
class modulepaths {
* return an array of global paths
* @returns array
function getmodulepaths() {
// setup the main module include() directories here
// note: could be placed in an xml config file later !!
$appdirs = array();
$appdirs[] = ''; // starting with the sub-application home directory
$appdirs[] = 'login';
$appdirs[] = 'login/classes';
$appdirs[] = 'login/tpl';
$appdirs[] = 'project';
$appdirs[] = 'project/classes';
$appdirs[] = 'project/tpl';
return $appdirs;
調用方法autoloader.php

// set the application path
$modulerootdir = 'd:/workspace/eh_plat_wms/dev_src'; // no trailing slash
// set the os type [optional] [unix|windows|mac] if we have
// trouble detecting the server os type. eg: path errors.
$ostype = 'windows';
// setup application class paths first
include 'lib/classpath.php';
// setup the module paths
include 'config/modulepaths.php';
$modulepaths = modulepaths::getmodulepaths();
$mpath = classpath::getclasspath($modulerootdir,$modulepaths, $ostype);
// retrieve and merge the php.ini path settings
$inipath = ini_get('include_path');
$cpath = classpath::concatpaths($mpath, $inipath, $ostype);
echo $cpath;
// and set the 'include_path' variables, as used by the file functions
ini_set('include_path', $cpath);
?>