PHP __autoload() not working – Smarty in effect

By | 31. January 2014

This evening I came across a very annoying problem while coding for my PHP+MySQL project. I decided to move all extensions used in this project to a folder, called “extensions”. This folder now contained some files, all containing classes. Instead of writing many “include” or “require” commands, I decided to use the PHP built-in autoload function:

After inserting that code, I got “Fatal error: Class ‘FileHelper’ not found in <file> on <line>“. Sort of… WTF? I made very sure that the file is located in the directory and double-checked for typos. No luck so far. Manually including the file still works fine. After some time I found out, that the __autoload() function is not even called. Okay… well… it is recommended to use the SPL methods anyway, so lets give it a try:

Guess what? Still a “Fatal error“. Investigating the same issue as above, this function is not called either. Searching the web just gave me hints like “autoload is not available in PHP CLI mode”. Well… thanks. However, I found the problem after some troubleshooting. Finally. It was Smarty, which was included right after defining the autoload methods, but before doing the actual function call, causing the error message. So… whats the problem?

Smarty also heavily relies on autoloading, thus it defines its own __autoload function. Per default, it uses __autoload without the SPL stuff, which indeed was causing the trouble. It just has overwritten my custom functions, which for sure resulted in the error above. Finally knowing that, and knowing that Smarty can be told to use the SPL methods, the solution is actually quite simple: Use the SPL stack to have multiple autoloader functions:

Simple solution for a simple problem with complex troubleshooting process, because the error messages do not help very much. So if someone else stumbles upon this problem, hopefully you can save some headache I had with that problem.


One thought on “PHP __autoload() not working – Smarty in effect

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.