Home > PHP Framework > ThinkPHP > body text

ThinkPHP configuration loading code optimization

咔咔
Release: 2020-10-09 16:08:24
Original
1399 people have browsed it

This article is mainly about Kaka’s optimization of the framework code. This is just Kaka’s optimization idea.

10. Framework underlying configuration loading code optimization

Inthinkphp/library/think The method loadFile in /Config.php seems a bit inelegant here.

Since the parse method is used at the end of the method, it is the factory mode mentioned before to load other types of configuration files.

So why not let this factory mode also load PHP and yaml type configuration files.

ThinkPHP configuration loading code optimizationAt this time, you need to create php files and yaml files in the thinkphp/library/think/config/driver directory.

First create a php file.

And imitating the other three types of files, in the php file type, you only need to determine whether it is a file, and then import the file.

In the framework, the PHP type configuration files are all in array form, so the parse method in the Php.php file can directly return the config attribute.

ThinkPHP configuration loading code optimizationStart the fun testing! The value of CONFIG_EXT was configured as yaml in the env file before.

That is to say, when reading the configuration file in the config directory, only files expanded to the yaml type can be read.

So you need to change this value back first, and you can use it after writing the yaml type factory class later.

For the convenience of testing, add a new configuration in the config directory

ThinkPHP configuration loading code optimizationThen comment out the code that determines the php and yaml types in the lodeFile method in the config class

ThinkPHP configuration loading code optimization
Insert picture description here

Read the config/app.php configuration in the controller

ThinkPHP configuration loading code optimizationPrint the result

Print There are no flaws in the results, which means that there is nothing wrong with the code we simply optimized. ThinkPHP configuration loading code optimizationThe same is true for yaml. You only need to convert the final data into an array and return it.

ThinkPHP configuration loading code optimizationThe above is the analysis process of Kaka's optimization of loading the underlying source code of the framework configuration file. If you have any discomfort, you can point it out in the comment area.

Persistence in learning, persistence in blogging, and persistence in sharing are the beliefs that Kaka has always adhered to since its beginning. I hope that Kaka’s articles on the huge Internet can bring you a little bit of help. I’m Kaka, see you next time.

The above is the detailed content of ThinkPHP configuration loading code optimization. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!