A little while ago, I had to build a .xls export function into a Yii application. I decided to use PHPExcel for this, since I had used it before. It’s also probably the best Excel importer/exporter for PHP. I soon figured that this didn’t go as smooth as I expected.
Yii kept throwing an Exception about it not being able to find the file to import. Of course it couldn’t find the file because PHPExcel’s importer was supposed to handle that. I searched around and found a couple of posts like this one. This failed for me because between the spl_autoload_unregister and spl_autoload_register calls, I had to get info from a Model through another Model. When you try to reach a related Model, Yii actually tries to autoload that Model. But since we just unregistered the yii autoloader by calling spl_autoload_unregister, the Yii import was never called and so autoloading the Model failed.
So I went on and figured out that the reason they unregistered the Yii autoloader, was because the Yii autoloader was called before the PHPExcel autoloader. Since the Yii autoloader throws an Exception when it can’t find a file and PHPExcel doesn’t, the solution was simple: switch the order in which the autoloaders get registered. I decided to do this by changing the PHPExcel Autoloader’s register method from this: