Home  >  Article  >  Backend Development  >  Memory location analysis of static properties and methods in PHP object-oriented_PHP tutorial

Memory location analysis of static properties and methods in PHP object-oriented_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:07:351030browse

Memory location analysis of static properties and methods in PHP object-oriented

This article mainly introduces the memory location of static properties and methods in PHP object-oriented, through the memory location Examples analyze the principles and usage techniques of static properties. Friends in need can refer to it

The example of this article analyzes the memory location of static properties and methods in PHP object-oriented. Share it with everyone for your reference. The details are as follows:

static The memory location of static properties -> class, not object. Let’s do 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 = "小妹";

public $height;

public function tell(){

}

}

echo Human:$name;

//不依赖于对象,就能直接访问。因为静态属性的内存位置是在类里,而不是对象。

$p1 = new Human();

$p2 = new Human();

print_r($p1);

echo $p1::$name = "夫人";

//在$p1对象上改变静态属性的值,那$p2对象也会相应的改变。

echo $p2::$name;

?>

1 2

3

4

5

6

7

8

9 10

1112 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 relying on objects. Because the memory location of static properties 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 results can be seen: 1. echo Human:$name: After the class is declared, there is a static attribute, which does not depend on the object. Therefore, there is only one static attribute (meaning that in memory, the storage location is not in the object; if it is in the object, then instantiate an object, and there will be the corresponding static location, such as the height attribute); 2. print_r($p1): The print result only has the height attribute, but no name; 3. After the value of a static attribute changes, the attribute value of all objects will be affected. Methods, whether static or ordinary, exist in the class memory space. The proof is also very simple, just create a new object and print_r (object). I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/955273.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/955273.htmlTechArticleMemory location analysis of static properties and methods in PHP object-oriented This article mainly introduces static in PHP object-oriented The memory locations of static properties and methods are divided by memory location instances...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn