ITeye博客

PHP5自动加载

netxdiy · 2007-07-30 22:28 · Markdown

<?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','<br/>');
}

/*------------------------------------------------------------------*/
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);
}

?>

<?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->includePaths,'.','lib','classes');

/*默认扩展名列表*/
array_push($this->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->containers)){
for($i=1;$i<$NumerOfArgs;$i++){
$arg=func_get_arg($i);
if(is_array($arg)){
foreach($argas$registerobj){
if(is_string($registerobj)&&!in_array($registerobj,array_values($this->$container_name))){
array_push($this->$container_name,$registerobj);
}
}
}else{
if(is_string($arg)){
array_push($this->$container_name,$arg);
}
}
}
}else{
$message='UnsupportedregistercontainerorfirstargumentisnotastringandmustbeoneofincludePaths,classNames,extentionNames.';
thrownewException($message);
}
}catch(Exception$e){

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

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

/**
*自动加载函数
*/
publicfunction__autoload(){

$list=array();

foreach($this->includePathsas$path){
foreach($this->classNamesas$class){
foreach($this->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->register('classNames',array('db','cd'),'Cloneable','user');
//var_dump(spl_autoload_functions());
//var_dump($test->includePaths,$test->classNames,$test->extensionNames);
//$cd=newCd();
//var_dump(get_included_files());
print_r(get_included_files());
//var_dump($test->__autoload());

?>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。