---
title: "PHP5自动加载"
author: "netxdiy"
date: "2007-07-30T22:28:00Z"
tags: []
slug: "php5"
draft: false
lastmod: "2012-09-27T11:21:31Z"
id: 1686730
url: "https://netxdiy.iteye.com/blog/1686730"
---

&lt;?php

/\*------------------------------------------------------------------\*/  
if(strtoupper(substr(PHP\_OS,0,3))==='WIN'){  
if(!defined('NEWLINE')){  
define('NEWLINE'," ");  
}  
}else{  
if(!defined('NEWLINE')){  
define('NEWLINE'," ");  
}  
}

/\*------------------------------------------------------------------\*/  
if(!defined('HTMLNEWLINE')){  
define('HTMLNEWLINE','&lt;br/&gt;');  
}

/\*------------------------------------------------------------------\*/  
if(!defined('CURRENT\_TIMESTAMP')){  
define('CURRENT\_TIMESTAMP',time());  
}  
if(!defined('FORMAT\_DATE')){  
define('FORMAT\_DATE','Y-m-d');  
}

if(!defined('TIMESTAMP')){  
define('TIMESTAMP','Y-m-dH:i:s');  
}

/\*------------------------------------------------------------------\*/  
if(!defined('SECONDS\_HOUR')){  
define('SECONDS\_HOUR',3600);  
}

if(!defined('SECONDS\_DAY')){  
define('SECONDS\_DAY',3600\*24);  
}

if(!defined('SECONDS\_WEEK')){  
define('SECONDS\_WEEK',3600\*24\*7);  
}

?&gt;

&lt;?php

require\_once'Constants.php';

classAutoload{

/\**  
\*注册容器,包括路径,类名和文件扩展名容器,可以赋默认值  
\*/  
public$containers=array(  
'includePaths',  
'classNames',  
'extensionNames'  
);  
/\*---------类搜索路径列表------------\*/  
public$includePaths=array();

/\*---------类名称列表------------\*/  
public$classNames=array();

/\*---------类文件扩展名列表------------\*/  
public$extensionNames=array();

publicfunction\_\_construct(){

/\*默认搜索路径\*/  
array\_push($this-&gt;includePaths,'.','lib','classes');

/\*默认扩展名列表\*/  
array\_push($this-&gt;extensionNames,'.php','.class.php');

/\*自动加载函数注册\*/  
if(method\_exists($this,'\_\_autoload')){  
spl\_autoload\_register(array($this,'\_\_autoload'));  
}

}

/\**  
\*注册类包含路径,类名称,扩展名称  
\*  
\*@paramstring$container\_name容器名称,路径容器名,类容器名和文件扩展容器名  
\*@parammixed可变参数列表  
\*@param........  
\*  
\*/  
publicfunctionregister(){

try{  
$NumerOfArgs=func\_num\_args();  
$container\_name=func\_get\_arg(0);  
if(is\_string($container\_name)&&in\_array($container\_name,$this-&gt;containers)){  
for($i=1;$i&lt;$NumerOfArgs;$i++){  
$arg=func\_get\_arg($i);  
if(is\_array($arg)){  
foreach($argas$registerobj){  
if(is\_string($registerobj)&&!in\_array($registerobj,array\_values($this-&gt;$container\_name))){  
array\_push($this-&gt;$container\_name,$registerobj);  
}  
}  
}else{  
if(is\_string($arg)){  
array\_push($this-&gt;$container\_name,$arg);  
}  
}  
}  
}else{  
$message='UnsupportedregistercontainerorfirstargumentisnotastringandmustbeoneofincludePaths,classNames,extentionNames.';  
thrownewException($message);  
}  
}catch(Exception$e){

$message=$e-&gt;getMessage();  
$msg='&lt;prestyle="font-family:verdana;font-size:12px;color:red;"&gt;'.$message.'&lt;/pre&gt;';  
echo$msg;

$message=$e-&gt;getTraceAsString();  
$msg='&lt;prestyle="font-family:verdana;font-size:12px;color:red;"&gt;'.$message.'&lt;/pre&gt;';  
echo$msg;  
}  
}

/\**  
\*自动加载函数  
\*/  
publicfunction\_\_autoload(){

$list=array();

foreach($this-&gt;includePathsas$path){  
foreach($this-&gt;classNamesas$class){  
foreach($this-&gt;extensionNamesas$ext){  
$filename=strtolower($path).DIRECTORY\_SEPARATOR.ucfirst($class).strtolower($ext);  
array\_push($list,$filename);  
if(file\_exists($filename)){  
require\_once$filename;  
}  
}  
}  
}

return$list;  
}

}

$test=newAutoload();

$test-&gt;register('classNames',array('db','cd'),'Cloneable','user');  
//var\_dump(spl\_autoload\_functions());  
//var\_dump($test-&gt;includePaths,$test-&gt;classNames,$test-&gt;extensionNames);  
//$cd=newCd();  
//var\_dump(get\_included\_files());  
print\_r(get\_included\_files());  
//var\_dump($test-&gt;\_\_autoload());

?&gt;