This article mainly focuses on the source code interpretation of obtaining configuration information, that is, the analysis of several methods of obtaining 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.
The framework provides several methods to obtain configuration information.
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
, you can directly use \Config::get('app_name'); to get it directly
So 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.
Kaka moved this code to the index controller, so that you can see it clearly It’s clear
Look at the print results first and make sure there are no problems
In 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!
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!