简体   繁体   中英

__autoload() Doesn't seem to do anything in PHP

I'm running the latest version of XAMPP and autoload() doesn't seem to be working as it should. I pretty much replaced my previous autoload with the one from the PHP manual, but to no avail.

After putting a few echo's and die()s, I've concluded that __autoload isn't being invoked at all.

class Main
{

    var $config_data;

    function __autoload($class_name) {
        echo "hello.";
        // If the file exists, require it
        if (file_exists(SYSTEMDIR.$class_name.".".EXT)) {
            echo 'Autoloader: The class exists.';
            (require_once(SYSTEMDIR.$class_name.".".EXT))
                or die("I tried to autoload class $class_name, but it failed! =(");
        } else {
            // The file didn't even exist. Die.
            die("I was going to autoload class $class_name, but it didn't exist! =(");
        }

    }

    /*
     * Function __construct
     * @param datatype variable description
     * @return datatype description
     */
   function __construct(/* $arg */) {
       //Load the config

       $this->config = new Config;

       //Load the uri class:
       $this->uri = new Uri;
   }

}

It doesn't output the "hello" that is located at the very top of __autoload().

The only output is:

Fatal error: Class 'Config' not found in E:\\xampplite\\htdocs\\system\\Main.php on line 84

AFAIK function __autoload must be defined outside the class. If you want to implement autoload function as part of a class, you should use callback and spl_autoload_register

那是因为您声明了Main->__autoload()函数(即类方法)而不是全局的__autoload()

__autoload doesn't go in the class. It's used to include the file the class is in.

You'll need to add an autoload function to the initial script to ever have it do anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM