<?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-d H: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’;
class Autoload {
/** * 注册容器,包括路径,类名和文件扩展名容器,可以赋默认值 */ public $containers = array ( ’includePaths’, ’classNames’, ’extensionNames’ ); /*--------- 类搜索路径列表 ------------*/ public $includePaths = array (); /*--------- 类名称列表 ------------*/ public $classNames = array (); /*--------- 类文件扩展名列表 ------------*/ public $extensionNames = array ();
public function __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’)); } }
/** * 注册类包含路径,类名称,扩展名称 * * @param string $container_name 容器名称,路径容器名,类容器名和文件扩展容器名 * @param mixed 可变参数列表 * @param........ * */ public function register() { 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 ($arg as $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 = ’Unsupported register container or first argument is not a string and must be one of includePaths,classNames,extentionNames.’; throw new Exception($message); } } catch (Exception $e) {
$message = $e->getMessage(); $msg = ’<pre style="font-family:verdana;font-size:12px;color:red;">’ . $message . ’</pre>’; echo $msg; $message = $e->getTraceAsString(); $msg = ’<pre style="font-family:verdana;font-size:12px;color:red;">’ . $message . ’</pre>’; echo $msg; } }
/** * 自动加载函数 */ public function __autoload() { $list = array(); foreach($this->includePaths as $path){ foreach($this->classNames as $class){ foreach($this->extensionNames as $ext){ $filename = strtolower($path) . DIRECTORY_SEPARATOR . ucfirst($class) . strtolower($ext); array_push($list, $filename); if(file_exists($filename)){ require_once $filename; } } } } return $list; } }
$test = new Autoload();
$test->register(’classNames’, array (’db’,’cd’), ’Cloneable’,’user’); //var_dump(spl_autoload_functions()); //var_dump($test->includePaths, $test->classNames, $test->extensionNames); //$cd = new Cd(); //var_dump(get_included_files()); print_r(get_included_files()); //var_dump($test->__autoload());
?>
|