下载帝国网站管理系统5.0正式版
 帝国论坛技术交流区网络编程PHP5自动加载实现方法及代码
帝国CMS5.0正式版发布
中客:空间产品随时可无条件退款!
测试:生成20万数据共用107分钟
《帝国下载系统》全面免费
Mysql大数据备份工具下载
帝国软件旗下新站 - 站长工具
1G双线帝国空间+200M数据库=158
帝国CMS教程下载
主题:PHP5自动加载实现方法及代码 [加入收藏夹]   

天浪歌
用户头衔:嘉宾

精华贴   :3
发贴数   :1725
经验值   :3930
注册时间:2006-09-14
信息 搜索 好友 发送悄悄话 《帝国网站管理系统》基础教程下载】   [第 1 楼]
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-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());



?>



ECMSer
有事就Q我    3260602    3260602@qq.com
帝国教程之猪头系列
2007-08-17 01:48:54 已设置保密 顶部 回复 引用 报告 编辑 删除


快速回复
内容

使用EBB代码
使用smile代码
显示签名
自动分析url
自动分析img
表情                   
                
    (按 Ctrl+Enter 直接提交)
    顶部  加入收藏夹
关于帝国 | 广告服务 | 联系我们 | 法律声明 | 隐私条款 | 许可协议
Powered by: EBB Version 2.2.1