Analysis of Methods of Using PHPExcel in Yii Framework

  • 2021-12-13 16:27:27
  • OfStack

This paper analyzes the method of using PHPExcel in Yii framework with examples. Share it for your reference, as follows:

PHPExcel is an easy-to-use class library for php to read excel files. Today, we encounter the problem of how to load PHPExcel class files in yii, because autoload mechanism of Yii installs class names to find files, that is, file names are corresponding class names, while PHPExcel class file naming method is: dir_dir_classname. php, that is, file names record the directory names of files, which is definitely not recognized by yii. What to do?

In fact, PHPExcel also has its own autoload method ( PHPExcel_Autoloader::load() ), by looking at the source code, it is also found through spl_autoload_register Function registered in the PHPExcel_Autoloader::register() And we know that the autoload mechanism of PHP is that all uses spl_autoload_register Function, all methods registered by autoload are spl_autoload_call Function is executed once, so we only need to register the autoload method of PHPExcel smoothly.

If you know the autoload mechanism of Yii, if you don't know it clearly, you can see the autoload mechanism of Yii in appendix. You can know that as long as you set it Yii::$enableIncludePath For false, the third-party class library has the opportunity to execute its own autoload method, and then load the PHPExcel class using the following two lines of code:


Yii::$enableIncludePath = false;
Yii::import('application.vendors.phpexcel.PHPExcel', 1);

force include is adopted in import, because PHPExcel. php will not register autoloader until new PHPExcel, and other classes such as PHPExcel_IOFactory will not be found if they are used before this.

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

1. https://www.ofstack.com/article/166128. htm. This method will first load Yii's own autoloader unregister, which will cause yii's own class not to load

2. https://www.ofstack.com/article/166132. htm. This method also modifies autoloader of PHPExcel, which is very costly.

Appendix: autoload mechanism of Yii

The Yii framework claims that its class loading method is very efficient and is really "loading in time". What is so special about it? Today, I studied the source code under 1, and found that it actually added 1 layer of "path cache" at the code level.

We know that to implement our own autoload method, we need to adopt spl_autoload_register() Function registers 1 autoload method, and this method registered by Yii is YiiBase::autoload() The logic of this method will be explained later. In addition, Yii1 is generally used Yii::import($pathAlias, $forceInclude=false) To load the corresponding class (this method directly calls the YiiBase::import() ), this method cooperates with YiiBase::autoload() You can realize "loading in time".

Let's talk about the general logic of import:

1. Check self::$_imports Whether the array has the corresponding $pathAlias, if there is a description has been loaded, directly return the class name or directory name; Otherwise, continue to step 2;

2. Obtain the actual pathname according to the path alias, and know whether the path alias to be loaded is a file according to whether the last part of the path alias is "*". If it is a file, go to step 3; Otherwise go to step 4;

3. If it is $forceInclude or true, require will be added to the file immediately and 1 entry will be added to the $_ imports array $alias => $className ; Otherwise, cache 1 entry in the array $classMap $className => $realPath ;

4. For a path, the path is cached in the array $_ includePaths, and an entry is added to the array $_ imports $alias => $realPath ;

5. End.

Because $forceInclude is false by default, import does not load the corresponding class immediately, but does not load it until it is used. This is YiiBase::autoload The work.

The general logic of autoload:

1. Check whether the class name has been cached in the array of $classMap or $_ coreClasses. If so, it will directly follow the corresponding file path of require, and $_ coreClasses is the mapping table of the framework's own class; Otherwise go to step 2;

2. Testing YiiBase::$enableIncludePath Whether it is false, if so, go to step 3, otherwise directly include($className . '.php')

3. Traverse the $includePaths array, splice the directory name with the class name, check whether it is a legal php file, if it is, include, and then jump out of the loop

4. End.

Note that the documentation states that if you want to use it with other class libraries 1, you must set $enableIncludePath to false, so that you can use it in the Yii::autoload() In case of failure, autoload methods of other class libraries have the opportunity to execute.

For more readers interested in Yii related contents, please check the topics on this site: "Introduction to Yii Framework and Summary of Common Skills", "Summary of Excellent Development Framework of php", "Introduction to smarty Template", "Introduction to php Object-Oriented Programming", "Summary of Usage of php String (string)", "Introduction to php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

Hope that this article is based on Yii framework of PHP programming help.


Related articles: