Joomla & PHPExcel > 1.7.2 Issue

Topics: Developer Forum
May 7, 2010 at 2:41 PM
Edited May 18, 2010 at 10:57 AM

While this is not specifically a PHPExcel issue I thought it might be useful for Joomla developers using the PHPExcel library to be aware of it. Depending on your version of Joomla (mine is currently 1.5.6) an autoloader incompatibilty exists between Joomla and PHPExcel.

Problem:

Basically, Joomla's <root>\libraries\loader.php file defines a function called __autoload which then PHP calls whenever it needs to load a class definition that is not currently in memory. Joomla's __autoload function when called searches through all the JLoader registered classes and loads the requested class from the defined file (if it can find it).

Anyway the issue is that the newer version of PHPExcel (> 1.7.2 more or less) uses a function called spl_autoload_register in the Classes\PHPExcel\Autoloader.php file that interferes/wipes out PHP's ability to use the Joomla defined __autoload function. So as soon as you attempt to load and use the PHPExcel class in Joomla PHP "forgets" how to load needed Joomla classes and a meltdown occurs. You'll get PHP errors that this or that Joomla class could not be found/loaded.

Fix: 

The easiest fix I found if to modify the Joomla <root>\libraries\loader.php file to replace the __autoload function with the following:

function Joomla_Autoload($class)
{ if(JLoader::load($class))
  { return true;
  }
  return false;
}
/*** specify extensions that may be loaded ***/
spl_autoload_extensions('.php, .class.php, .lib.php');
/*** register the loader functions ***/
spl_autoload_register('Joomla_Autoload');

I commented out the __autoload function just in case I needed it back for any reason.

Notes:

The spl_ functions represent a better way to deal with autoloading classes (let's PHP handle multiple autoload functions) so I understand PHPExcel switching to it. Obviously they have no control (or notion perhaps) as to how Joomla deals with autoloading and that this would interfere in any way. It's really Joomla's issue for assuming their autoloader would be the only one in use. Regardless there is talk of this being changed in subsequent versions of Joomla so by time you read this it may be changed. Just look in the <root>\libraries\loader.php to see how autoloading is being dealt with and respond accordingly.

Hope this helps someone. Good luck!

Coordinator
May 15, 2010 at 11:11 AM

Thanks for posting this advice.

Sep 8, 2010 at 8:53 AM

Thanks so much! I very happy with this guide.I not good English. So I can say that I can import/export excel file by PHPExcel in Joomla and by manipulation with database.

Dec 10, 2010 at 1:41 PM

Thank you, was very helpful.

Aug 30, 2012 at 5:05 AM

where should I put these lines -

/*** specify extensions that may be loaded ***/
spl_autoload_extensions('.php, .class.php, .lib.php');
/*** register the loader functions ***/
spl_autoload_register('Joomla_Autoload');
??

i'm using Joomla! version, 2.5.6 and PHPExcel-1.7.7
any respond would be greatly appreciated..
=) 

Jul 19, 2014 at 7:10 AM
Edited Jul 19, 2014 at 7:11 AM
How to know that PHPExcel autoload confict with joomla or any other application? I run the PHPExcel library and testing it under root directory of magento.