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.