V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
zencoding
V2EX  ›  问与答

对php spl_autoload_register的疑问

  •  
  •   zencoding · 2013-08-03 08:25:35 +08:00 · 2630 次点击
    这是一个创建于 3941 天前的主题,其中的信息可能已经有所发展或是发生改变。
    init.inc.php的内容

    //自动挂载类
    function AutoLoad($classname){
    // class类
    $filepath = BASE_CLASS . $classname . '.class.php';
    if (file_exists($filepath)) {
    return include $filepath;
    }
    //lib库文件
    $filepath = BASE_LIB . $classname . '.lib.php';
    if (file_exists($filepath)) {
    return include $filepath;
    }
    }
    spl_autoload_register('AutoLoad');

    在建一个test.class.php放在class文件夹下
    class test{
    function method(){
    echo 'Hello world';
    }
    }


    然后index.php

    require 'init.inc.php';
    AutoLoad('test');
    $test = new testclass();
    $test->method();

    如果在init.inc.php中注释掉spl_autoload_register('AutoLoad');
    一样成功,问题就来了,spl_autoload_register有什么用呢?
    3 条回复    1970-01-01 08:00:00 +08:00
    donwa
        1
    donwa  
       2013-08-03 08:32:30 +08:00 via iPhone   ❤️ 1
    不用autoload('test');
    找不到类它会自己执行autoload
    zencoding
        2
    zencoding  
    OP
       2013-08-03 08:39:19 +08:00
    @donwa 前提是类文件名和类名要一致
    xiaokai
        3
    xiaokai  
       2013-08-03 10:58:22 +08:00
    这不应该成功嘛 $test = new testclass(); ?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2464 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 06:28 · PVG 14:28 · LAX 23:28 · JFK 02:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.