PHP autoload and smarty 3
I was setting up for some code today. Code which would use, among other things, Smarty 3 since that’s a thing now apparently. (It used to be Smarty 2, but I guess times change and numbers change.)
There’s an __autoload function that I wrote a while ago and often use to, you know, automatically include files when instantiating classes. But it didn’t work at all.
The file itself with the __autoload function included fine, but autoload function was not being called. It was pretty confusing, to be honest.
Jeff and I eventually traced this back to one line in my code — the line including the Smarty source file.
So I had a look in there and found this:
/** * register the class autoloader */ if (!defined('SMARTY_SPL_AUTOLOAD')) { define('SMARTY_SPL_AUTOLOAD', 0); } if (SMARTY_SPL_AUTOLOAD && set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false) { $registeredAutoLoadFunctions = spl_autoload_functions(); if (!isset($registeredAutoLoadFunctions['spl_autoload'])) { spl_autoload_register(); } } else { spl_autoload_register('smartyAutoload'); }
Which looked suspiciously culpable. So I added this line to my code:
spl_autoload_register('__autoload');
And then it worked!