Difference between #2 and #5 of
How to use PHPExcel external library with Yii

Changes

Title unchanged

How to use PHPExcel external library with Yii

Category unchanged

How-tos

Yii version unchanged

Tags changed

yii, libraries, PHPExcel, create excel file with phpexcel

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://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.
27 1
25 followers
Viewed: 112 594 times
Version: 1.1
Category: How-tos
Written by: Antonio Ramirez
Last updated by: cgsmith
Created on: Nov 5, 2010
Last updated: 5 years ago
Update Article

Revisions

View all history