1

I have strange problem with autoload function. I have this code:

function __autoload($class) {
    echo "in autoload function: ".$class."<br/>";
    require_once $class.".php";
}
TestClass::testMethod();
echo is_file("Debug.php") ? " file exist " : "file not exist";
echo "<br/>";
echo  class_exists('Debug') ? "class exist" : "class not exist";
Debug::getIncludeExecutionTime($include, $time);

And output is:

in autoload function: TestClass
file exist
class not exist
Fatal error: Class 'Debug' not found in Z:[my local host path] on line 207

So, there is misseed 'in autoload function Debug'. Most strangest there - if I invoke Debug class in other function, or in other place, autoload work.

Why autoload function was not invoked? What can be reasons? There is no spl_autoload_register functions on project.

2
  • There is no namespaces Commented Mar 23, 2015 at 12:37
  • I found solution, that was Smarty template engine. Commented Mar 23, 2015 at 13:03

1 Answer 1

1

There are 2 things. First if you work with namespaces you have to include the correct namespace. You can make a var_dump on your $class variable to look if the classname is correct.

The other thing is that you should verify that your path is really correct. Perhaps you should specify the complete path to your file with __DIR__ or dirname(__FILE__).

function __autoload($class) {
    echo "in autoload function: ".$class."<br/>";
    if(is_file(__DIR__.'/'.$class.".php")) {
        require_once __DIR__.'/'.$class.".php";
    }
}

And perhaps you should check on that point if the file exists.

Sign up to request clarification or add additional context in comments.

4 Comments

there is no namepaces, and file is exist, because is_file("Debug.php") return true. There is weird behavior - at first script must invoke autoload, and if it does not found class, throw an error. But in my case script throws error without autoload function.
Hmm... have you defined a class in your debug file? Sounds a bit stupid :D but sometimes the solution is very simple.
I found what exactly goes wrong - Smarty autoload kill my autoload.
Hmm ok ... thats a problem if you have another function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.