The memory location analysis of static property and method in PHP object-oriented
This article mainly introduces the memory location of static property and method in object-oriented in PHP, analyzes the principle and use technique of static property by using the memory location instance, the friend who needs can refer to
In this paper, we analyze the memory location of static property and method in PHP object-oriented. Share to everyone for your reference. Specific as follows:
Static property of the memory location-class, not the object. Here's 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 = "little Sister"; Public $height; Public function tell () { } } echo Human: $name; It can be accessed directly without being dependent on the object. Because the memory location of the static property is in the class, not the object. $p 1 = new Human (); $p 2 = new Human (); Print_r ($p 1); echo $p 1:: $name = "Madame"; Changes the value of the static property on the $p1 object, and the $p2 object changes accordingly. echo $p 2:: $name; ?> |
The output can be shown as follows:
1, Echo Human: $name: class declaration is complete, there is a static property, not dependent on the object. Therefore, there is only one static property (meaning that in memory, the location is not in the object; If you instantiate an object in the object, it has the static position, such as the Height property);
2, Print_r ($p 1): Print results only the Height property, and no name;
3. When the value of a static property changes, all objects will be affected by that property value.
For a method, whether static or normal, it exists in the class memory space. The proof is also very simple, new object, Print_r (object) can be.
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/955273.html www.bkjia.com true http://www.bkjia.com/PHPjc/955273.html techarticle analysis of the memory location of static properties and methods in object-oriented PHP This article mainly introduces the memory location of static properties and methods in object-oriented PHP, which is divided by the memory location instance ...