V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
zencoding
V2EX  ›  问与答

对php spl_autoload_register的疑问

  •  
  •   zencoding · Aug 3, 2013 · 3135 views
    This topic created in 4663 days ago, the information mentioned may be changed or developed.
    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 replies    1970-01-01 08:00:00 +08:00
    donwa
        1
    donwa  
       Aug 3, 2013 via iPhone   ❤️ 1
    不用autoload('test');
    找不到类它会自己执行autoload
    zencoding
        2
    zencoding  
    OP
       Aug 3, 2013
    @donwa 前提是类文件名和类名要一致
    xiaokai
        3
    xiaokai  
       Aug 3, 2013
    这不应该成功嘛 $test = new testclass(); ?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2958 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 07:26 · PVG 15:26 · LAX 00:26 · JFK 03:26
    ♥ Do have faith in what you're doing.