开发者

Weird issue where __autoload stops working

I'm having the weirdest problem. I have an __autoload function that handles all my class including. At one point in my code, namely between new XLSReader() and new CVSReader(), the __autoload function just stops being used. Consequently I'm getting class CSVReader not found errors. This is the code where __autoload stops working

// Get general data
printf("Fetching data from \"%s\"... ", $data_file);
$csvreader = new \XLSReader($data_file, $columnsToFetch);
$data = $csvreader->getData();
print("Done.\n");
开发者_高级运维
// Get IP data
print("Loading IP addresses... ");
$csvreader = new \CSVReader($ip_file, null);
$ip_data = $csvreader->getData();
print("Done.\n");

I know the __autoload function has stopped working, because I manually included the CSVReader class and got not found errors on the next class that should have been auto-loaded.

To make it clear, before the above code snippet, auto-loading is working just like it should. Also, here is the __autoload function

// Autoload
function __autoload($classname)
{
    $classname = str_replace("\\", "/", $classname);

    $path = "code/" . $classname . ".php";

    if(is_file($path))
    {
        include($path);
        return true;
    }
    else
    {
        return false;
    }
}

Any ideas?


Might you be running into the murky waters of using __autoload() instead of spl_autoload_register()?

http://php.net/manual/en/function.spl-autoload-register.php

It might also be a case issue, or the non-absolute file path that you're using in your autoload function.

If the latter, prepend $path with __DIR__ (or dirname(__FILE__)), or whatever is needed in your setup:

$path = __DIR__ . $path; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜