I followed Xin Xing's in-depth discussion on PHP's reflection mechanism. as early as I learned Java, I clearly remember the reflection after I learned multithreading. now, of course, there is also a reflection mechanism in PHP, if you still don't understand shoes, you need to understand. after all, the reflection function is quite powerful and learning it is of practical significance. In simple understanding, reflection gets a class and obtains some information about the class, such as the methods and parameters of the class, of course, we can also dynamically call its methods and other functions. its purpose is to automatically load plug-ins, automatically generate documents, and so on, so as to expand the PHP language.
Almost all reflection classes implement the reflector interface. all implementation classes have a method, that is, the export method. we can use this method to view some information, here we take the PHP built-in class as the first example to look at the basic usage of reflection. we create a PHP file with the code as follows:
Below is the output information of this part:
Class [ class mysqli ] { - Constants [0] { } - Static properties [0] { } - Static methods [1] { Method [ static public method poll ] { - Parameters [5] { Parameter #0 [ array or NULL &$read ] Parameter #1 [ array or NULL &$write ] Parameter #2 [ array or NULL &$error ] Parameter #3 [ $sec ] Parameter #4 [ $usec ] } } } - Properties [19] { Property [ public $affected_rows ] Property [ public $client_info ] Property [ public $client_version ] Property [ public $connect_errno ] Property [ public $connect_error ] Property [ public $errno ] Property [ public $error ] Property [ public $error_list ] Property [ public $field_count ] Property [ public $host_info ] Property [ public $info ] Property [ public $insert_id ]
Here I just intercepted some of the content, because the whole content is still quite long, we can see that it does not define any static attributes, it has a static method, the method name is poll, it requires five parameters. The first of these five parameters can be an array or a NULL value, which is represented by the variable $ read, the second parameter is an array or NULL .... Here we will not list them one by one. readers can read the code snippet above and make their own judgments.
Next, let's talk about what our code has done. First, we define a reflection class ReflectionClass instance $ class. we can use var_dump to view its information, here, I will not paste the information, and check whether the reader has performed the operation in person. then we call the Reflection static method export to export the information of this class. then we can see the above information.
The reflection mechanism is used to view some information about the built-in class. can we check the custom class? the answer is obviously yes, for example, the following code:
IsUserDefined () {Reflection: export ($ myclass );}}
Then we run the above code and find that the output information is as follows: Class [class Person] {@ D: \ MyApp \ wamp \ www \ ap. php 2-10-Constants [0] {}-Static properties [0] {}-Static methods [0] {}-Properties [1] {Property [public $ name]} -Methods [1] {/*** is used only to print information */Method [public method test] {@ D: \ MyApp \ wamp \ www \ ap. php 7-9 }}}
Through its reflection mechanism, we can see that it is still quite comprehensive. for example, it does not define constants or static attributes, there is a public attribute, it is $ name, and there is another method, it is called test, and the comment of this method is "/*** only used to print information */". can you further understand the function of writing a good comment, in this way, when others reflect your class, you will be able to see the role of this function. By the way, get_declared_classes is used to obtain the defined class. Note that the above $ myclass is a class, not a string, therefore, it has its own method to check whether isUserDefined is called by the user. Some children's shoes may say that we can also get some information about the class through var_dump. that's right, it's the same class. let's look at what will be output with var_dump. the code is as follows:
Name = "xinxing"; var_dump ($ person );
Its output is as follows:
object(Person)[1] public 'name' => string 'xinxing' (length=7)
Of course, you may also know that var_dump is inferior to reflection in class operations. it can only view the information of class instances and only view class attributes, there is no way to do anything about the annotations and methods in the class. this is not what the object should have.
Don't go away. my blog will continue to write an application about reflection. this is a bit long. I just want to open another blog and look forward to your attention.