Php class member variables are not declared. Solution

Source: Internet
Author: User
The member variables of the php class do not declare class & nbsp; test {& nbsp; private & nbsp; $ name; & nbsp; & nbsp; private & nbsp; $ sex; & nbsp php member variables are not declared

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? Php object-oriented sharing:
------ Solution --------------------
Dynamically generated object attributes are global, which is the same as variables that can be accessed without being declared.
However, due to some academic criticism, php began to give up this lightweight expression method.
Therefore, try not to do this.

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.