PHP Reflection class and ReflectionObject usage _ PHP Tutorial

Source: Internet
Author: User
PHP Reflection class and ReflectionObject usage method. PHP extension reflection class, which is used to analyze php programs, export or extract detailed information about classes, methods, attributes, parameters, and so on, including comments. Looking at this problem, the extension reflection class in php Class PHP is used to analyze php programs and export or extract detailed information about classes, methods, attributes, parameters, etc, including comments.
Let's look at this question. the member variables of the php class are not declared in the class, but in the function. what is the difference?

The code is as follows:


Class test {
Private $ name;
Private $ sex;
Function _ construct (){
$ This-> aaa = 'aaa ';
}
}

$ Test = new test ();

$ Reflect = new ReflectionClass ($ test );
$ Pro = $ reflect-> getDefaultProperties ();
Print_r ($ pro); // print the result: Array ([name] => [sex] =>)

Echo $ test-> aaa; // print the result: aaa


In this test class, two member variables $ name and $ sex are declared, but in the constructor, another variable $ aaa is declared, and the class is initialized, using the reflection class to print the default member attribute only has two declared member variable attributes, but the $ aaa variable of the print class can still be output.
Do I have to declare the member variables of the class? Can I declare them in the function? what is the difference?
In your example, using ReflectionClass is inappropriate, because _ construct is executed only when class is instantiated.
That is to say, ReflectionClass is more about the structure during reflection class declaration than the structure after class instantiation. Therefore, it is true that no output attribute aaa is correct because the attribute aaa is indeed (during class declaration) does not exist.
In this case, attributes aaa should be reflected in the instantiated structure using ReflectionObject, for example

The code is as follows:


Class test {
Private $ name;
Private $ sex;
Function _ construct (){
$ This-> aaa = 'aaa ';
}
}
$ Test = new test ();

$ Reflect = new ReflectionObject ($ test );
$ Pro = $ reflect-> getProperties ();
Print_r ($ pro );


After instantiation, the property aaa will exist, and you will be able to see the property aaa.
Because php is a "dynamic" language, you do not need to declare the member variables of a class. it is also possible to declare them in functions.

Bytes. Let's look at this problem. php class...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.