PHP5自动加载

<?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());



?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值