Home>Article>PHP Framework> ThinkPHP container binding parameters

ThinkPHP container binding parameters

咔咔
咔咔 Original
2020-10-12 12:58:50 1924browse

This article is mainly about learning how the __make method is executed in each method, and now that we have learned about the container, how can we use the container to call the configuration to obtain the configuration information? .

Analyze the bindParams method

Next, let’s analyze thebindParamsmethod.

Regarding parameter passing, it is a reflection class. The second parameter will not be explained for the time being. There is no response scenario yet.

The first parameter value$reflect

##Use the reflection method$reflect->getNumberOfParameters( )Get the number of parameters in the corresponding method in the reflection class. According to the above, it is the __make method. The number of parameters of only two methods has been obtained in the container code, one is the __make method, and the other is the constructor in the reflection class.

Since there is currently no scenario for passing vars variables, we will skip this section without studying it for the time being.

Code$params = $reflect->getParameters();also uses reflection to obtain the parameters of the method.

The results you can see when printing are two sets of data.

So where does this set of data come from? Scroll up and take a look at what the$reflectparameter is and you will understand.

think\App This reflection class does not have a __make method, so the parameters in the constructor will be obtained.

Then there is a __make method in the think\Log reflection class, so the parameters of __make will be returned, as shown below.

Just like a class like think\Log, which has both a __make method and a constructor, it will go twicebindParamsmethod , you should all understand this, it is exactly the logic in the picture below.

The next step is the parameters obtained in the loop reflection class.

Get the parameter name, and get the corresponding reflection class

Finally, pass the obtained reflection class to thegetObjectParammethod.

There is not much content in thisgetObjectParammethod.

Since$varsis an empty array from beginning to end, the operation of removing the first element of the array and determining whether it is a closure will not be executed.

The make method will eventually be executed upon return

Then the make method will return the instance directly from the container

When a reflection class exists __make method,return $method->invokeArgs(null, $args);will eventually be executed, and the reflection class method will be executed with parameters

Use the container to call Configuration class

Now that I have read the container source code once, can I use the container to implement it?

Of course it is possible. Here you need to pay attention to the namespace of Kaka. For the convenience of future review, the class name is also named Container, so I added an alias. The one you are using Time is not needed!

As of now, the source code of the container is almost explained. Later, Kaka will make a complete flow chart for everyone to view.

Persistence in learning, persistence in blogging, and persistence in sharing are the beliefs that Kaka has always upheld since his career. 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 container binding parameters. 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