Home  >  Article  >  PHP Framework  >  Reflection mechanism of ThinkPHP container

Reflection mechanism of ThinkPHP container

咔咔
咔咔Original
2020-10-10 11:29:391777browse

Reflection has an importance in the container that you cannot understand without looking at the source code. Be sure to read this article carefully

IV , Must know the reflection mechanism

I don’t know if you have ever understood the reflection mechanism of GO. To be honest, I felt a little dizzy after seeing the reflection mechanism of GO at that time. .

But after looking at PHP's reflection later, I not only had a certain in-depth understanding of go's reflection, but also had a better understanding of PHP's reflection.

The concept of reflection was introduced in PHP5.0. Among the currently used frameworks, thinkphp and laravel both use reflection to implement dependency injection.

Understanding of reflection: In fact, it is to obtain things other than the root from the root. In programming, as long as you know a class, you can know all the properties and methods of this class.

Case

This is just a simple implementation case to obtain all methods and properties of the class. You can see whether the print result in the picture below is consistent with TestReflection.

Reflection mechanism of ThinkPHP containerReflection mechanism of ThinkPHP container

#This also presents a problem from the side, that is, some information that should not be exposed will be exposed.

There are many more interfaces provided by reflection. Here are a few commonly used ones, and the rest are parsed in the framework source code.

Use reflection to execute a class method

The printed result is click

Reflection mechanism of ThinkPHP containerUse reflection to execute a class method Methods with parameters

Reflection mechanism of ThinkPHP container
Insert picture description here

Use reflection to execute methods without parameters in a class

Reflection mechanism of ThinkPHP containerYou can try other methods yourself, because this reflection interface is not very used in basic development. What Kaka introduces to you can be used later when reading the source code. arrived.

Now that we understand reflection, what can reflection do? There is a function point that automatically generates documents.

Reflect here, let’s take a brief look at it. If you want to know more about the use of interfaces, you can go to the official website to view the corresponding interface information.

Reflection mechanism of ThinkPHP container
Insert picture description here

After understanding reflection, we will start to get to the point, and we need to officially enter our container link. Only by laying the foundation above can the following containers be better understood.

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 Reflection mechanism of ThinkPHP container. 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