Memory location analysis of static attributes and methods in php object orientation. Php object-oriented static attributes and method memory location analysis this article mainly introduces the static attributes and method memory location in php object-oriented, memory location instances are used to analyze static attributes and methods in php object-oriented memory locations.
This article mainly introduces the memory location of static attributes and methods in php object orientation. it analyzes the principle and usage skills of static attributes through memory location instances. For more information, see
This article analyzes the memory location of static attributes and methods in php object orientation. Share it with you for your reference. The details are as follows:
Static attribute memory location --> class, not an object. The following is a test to prove it.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Header ("content-type: text/html; charset = utf-8 "); Class Human { Static public $ name = "Xiaomei "; Public $ height; Public function tell (){ } } Echo Human: $ name; // Access directly without relying on objects. Because the memory location of the static attribute is in the class, not the object. $ P1 = new Human (); $ P2 = new Human (); Print_r ($ p1 ); Echo $ p1: $ name = "Mrs "; // Change the value of the static attribute on the $ p1 object, and the $ p2 object will also change accordingly. Echo $ p2: $ name; ?> |
The output result shows that:
1. echo Human: $ name: After the class declaration is complete, the static attribute exists and does not depend on the object. Therefore, there is only one static attribute (in the memory, the storage location is not in the object; if it is in the object, it instantiates an object, the corresponding static location, such as the height attribute );
2. print_r ($ p1): the printed results only have the height attribute, but not the name;
3. when the value of a static attribute changes, the attribute value of all objects will be affected.
For methods, both static and common methods exist in the class memory space. The proof is also very simple. a new object, print_r (object), is enough.
I hope this article will help you with php programming.
This article describes the memory location of static attributes and methods in php object orientation...