Use phpexcel in yii framework

Source: Internet
Author: User
Tags autoload

Phpexcel is a useful PHP class library for reading Excel files. Today, I encountered a problem about how to load phpexcel files in yii, because yii's autoload mechanism is to install class names to find files, that is, the file name is the corresponding class name, while the class file naming method of phpexcel is dir_dir_classname.php, that is, the file name records all the directory names of the file. This naming method yii cannot recognize. What should I do?

In fact, phpexcel also has its own autoload method (phpexcel_autoloader: load (). By checking the source code, it is also registered through the spl_autoload_register function (in phpexcel_autoloader: Register ), we know that the PHP autoload mechanism is that all methods registered using the spl_autoload_register function will be executed by the spl_autoload_call function during autoload, therefore, we only need to register the autoload method of phpexcel.

If you understand the autoload mechanism of yii, you can refer to the autoload mechanism of yii if you are not clear about it. As long as you set yii ::$ enableincludepath to false, the third-party Class Library has the opportunity to execute its own autoload method, and then use the following two linesCodeYou can load the phpexcel class:

Yii: $ enableincludepath = false; yii: Import ('application. Vendors. phpexcel. phpexcel ', 1 );

The force include method is used for import, because phpexcel. PHP registers autoloader only when it is require. If it is registered only when new phpexcel is used, if other classes such as phpexcel_iofactory are used before this, the error of class not found will occur.

I personally think this method is more convenient and elegant. It is much better than other methods on the Internet. The methods listed below are more or less problematic, for example:

1. When unregister is merged, the yii class cannot be loaded.

2. Renewal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.