日期:2014-05-17  浏览次数:20621 次

PHP 自动加载对象(以MVC框架为例)
<?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' );
		spl_autoload ( $class );
	}
	
	public function model($class) {
		$class = preg_replace ( '/_model$/ui', '', $class );
		
		set_include_path ( get_include_path () . PATH_SEPARATOR . '/model/' );
		spl_autoload_extensions ( '.model.php' );
		spl_autoload ( $class );
	}
	
	public function helper($class) {
		$class = preg_replace ( '/_helper$/ui', '', $class );
		
		set_include_path ( get_include_path () . PATH_SEPARATOR . '/helper/' );
		spl_autoload_extensions ( '.helper.php' );
		spl_autoload ( $class );
	}

}

//call
autoloader::init ();
?>

1, 在程序使用未声明的类时会自动调用 __autolaod() 函数来加载;

<?php
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文档

<?php
/*
* $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