This article mainly introduces the memory location of static attributes and methods in php object orientation, and analyzes the principles and usage skills of static attributes through memory location instances, for more information, see the examples in this article to analyze the static attributes of php object and the memory location of the method. 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.
<? Phpheader ("content-type: text/html; charset = utf-8"); class Human {static public $ name = "Xiaomei"; public $ height; public function tell () {}} echo Human: $ name; // directly accessible 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.