Home  >  Article  >  PHP Framework  >  ThinkPHP analysis to obtain configuration source code analysis

ThinkPHP analysis to obtain configuration source code analysis

咔咔
咔咔Original
2020-10-10 11:12:061591browse

This article mainly focuses on the source code interpretation of obtaining configuration information, that is, the analysis of several methods of obtaining configuration

11. Analysis How to get config How to get configuration

We all know that when obtaining configuration information, we can directly use \Config::get() to obtain the configuration file. Information.

Next, Kaka will analyze the process of obtaining configuration.

ThinkPHP analysis to obtain configuration source code analysisThe framework provides several methods to obtain configuration information.

  • \Config::get('configuration parameters');
  • \Config::get('configuration file');
  • \Config::pull('Configuration File');

It is estimated that very few people use the first method. The way is to directly obtain the corresponding configurations in all configuration files.

For example: If you want to get the application name configuration in the config directory

ThinkPHP analysis to obtain configuration source code analysis, you can directly use \Config::get('app_name'); to get it directly

ThinkPHP analysis to obtain configuration source code analysisThinkPHP analysis to obtain configuration source code analysisSo what is this process like?

When directly obtaining the configuration parameters, the only code flows are these two.

The first paragraph is to add the prefix app

The second paragraph is to loop through the config file to obtain data.

If you debug this code directly with breakpoints, you won’t see any effect. If Kaka moves this code outside for everyone to execute, you will see it clearly.

ThinkPHP analysis to obtain configuration source code analysis
Insert picture description here

ThinkPHP analysis to obtain configuration source code analysisKaka moved this code to the index controller, so that you can see it clearly It’s clear

ThinkPHP analysis to obtain configuration source code analysisLook at the print results first and make sure there are no problems

ThinkPHP analysis to obtain configuration source code analysisIn fact, if the code here is executed in the source code, you will see a lot of other information, which will be very Affects the interpretation of information.

But after we transplant it, we can ensure that there is no other complicated information when the code is running, which is conducive to the correct interpretation of the information.

Then look at this code immediately. When I looked at this code before, it seemed like nothing, but the more you look at it, the more you will find that the design of this code is very excellent.

Why do you say this!

First of all, this code will go through the first loop to execute the app. This execution will obtain the configuration information with the key value app in all configs.

Then assign the value to the config variable again, and execute the second loop as app_name.

The data obtained in the loop here is obtained based on the data obtained in the first loop. That is, the second time is the data obtained under $config['app'].

This shows how well designed this code is!

ThinkPHP analysis to obtain configuration source code analysis As for the other two methods, I leave them to you. You can simply try to transplant the code like Kaka, and then interpret it step by step.

You will discover the beauty of the code. If you read more, it will provide you with a lot of ideas for writing your own code in the future.

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 analysis to obtain configuration source code analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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