Revision #3 has been created by François Gannaz on May 27, 2013, 6:32:43 AM with the memo:
Better solution than disabling Yii's autoload
« previous (#2) next (#4) »
Changes
Title
unchanged
How to use PHPExcel external library with Yii
Category
unchanged
How-tos
Yii version
unchanged
Tags
unchanged
yii, libraries
Content
changed
Trying to learn and help some other programmers to find their solutions on the [Yii Forum](http://www.yiiframework.com/forum "Yii Forum") (I think is a superb way to learn the framework), I was facing one challenge with an external library that a fellow programmer wanted to use -quite good indeed: [PHPExcel](http://phpexcel.codeplex.com/ "PHPExcel"). And what is PHPExcel?
<h4>PHPExcel - OpenXML - Create Excel2007 documents in PHP - Spreadsheet engine</h4>
<blockquote>Project providing a set of classes for the PHP programming language, which allow you to write to and read from different file formats, like Excel 2007, PDF, HTML, ... This project is built around Microsoft's OpenXML standard and PHP.
Checkout the [Features](http://phpexcel.codeplex.com/wikipage?title=Features
"PHPExcel") this class set provides, such as setting spreadsheet meta data (author, title, description, ...), multiple worksheets, different fonts and font styles, cell borders, fills, gradients, adding images to your spreadsheet and much, much more!</blockquote>
<h2>The Challenge</h2>[...]
```
<h2>First Solution
: disabling Yii's Autoload</h2>
The workaround to this problem (at least the one I know) is by making use of the [spl_autoload_register](http://php.net/manual/en/function.spl-autoload-register.php "spl_autoload_register") and [spl_autoload_unregister](http://www.php.net/manual/en/function.spl-autoload-unregister.php "spl_autoload_unregister") PHP's functions. The following code shows how we got the library working -I assume that you have [downloaded the PHPExcel](http://phpexcel.codeplex.com/ "PHPExcel") files, unzipped its contents and place them into a **phpexcel** folder within your application's **protected/extensions** folder:[...]
```
<h2>Second solution: patching PhpExcel</h2>
The previous solution has an annoying consequence: one can't easily mix Yii's classes within a code that uses PHPExcel. Fortunately, there is a solution with no such side effect.
In "Classes/PHPExcel/Autoloader.php", replace the line
```php
return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'));
```
with:
```php
return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'), true, true);
```
See the documentation of [spl_autoload_register](http://us.php.net/manual/en/function.spl-autoload-register.php) for a description of these parameters.
<h2>Final Words</h2>
This post is using PHPExcel external library as an example, but this procedure should be taken into account when we encounter a problem like this. Whenever you find a library that you wish to include in your Yii application, check out its autoloading functions first.