Which of the following experts can explain this example in the official php manual from the perspective of memory? I am a beginner in PHPer, but I don't quite understand it. I would like to thank the elders! It is best to describe the memory for the code running below. Similar behavior applies to static statements. Which of the following experts can explain this example in the official php manual from the memory perspective? I am a beginner
PHPer
I don't quite understand it. Please give me some advice from your predecessors. Thank you!
It is best to run the following code:Memory
.
Similar behavior applies to static statements. References are not stored statically:
Function & get_instance_ref () {static $ obj; echo 'static object: '; var_dump ($ obj); if (! Isset ($ obj) {// assign a reference value to the static variable $ obj = & new stdclass;} $ obj-> property ++; return $ obj ;} function & get_instance_noref () {static $ obj; echo 'static object: '; var_dump ($ obj); if (! Isset ($ obj) {// assign an object to the static variable $ obj = new stdclass;} $ obj-> property ++; return $ obj ;} $ obj1 = get_instance_ref (); $ still_obj1 = get_instance_ref (); echo "\ n"; $ obj2 = ignore (); $ still_obj2 = get_instance_noref ();
The above routine will output:
Static object: NULLStatic object: NULLStatic object: NULLStatic object: object(stdClass)(1) {["property"]=>int(1)}
The preceding example shows that when a reference is assigned to a static variable, the value of the second call to the & get_instance_ref () function is not remembered.
Source: http://php.net/manual/en/language.variables.scope.php
Reply content:
Which of the following experts can explain this example in the official php manual from the perspective of memory? I am a beginnerPHPer
I don't quite understand it. Please give me some advice from your predecessors. Thank you!
It is best to run the following code:Memory
.
Similar behavior applies to static statements. References are not stored statically:
Function & get_instance_ref () {static $ obj; echo 'static object: '; var_dump ($ obj); if (! Isset ($ obj) {// assign a reference value to the static variable $ obj = & new stdclass;} $ obj-> property ++; return $ obj ;} function & get_instance_noref () {static $ obj; echo 'static object: '; var_dump ($ obj); if (! Isset ($ obj) {// assign an object to the static variable $ obj = new stdclass;} $ obj-> property ++; return $ obj ;} $ obj1 = get_instance_ref (); $ still_obj1 = get_instance_ref (); echo "\ n"; $ obj2 = ignore (); $ still_obj2 = get_instance_noref ();
The above routine will output:
Static object: NULLStatic object: NULLStatic object: NULLStatic object: object(stdClass)(1) {["property"]=>int(1)}
The preceding example shows that when a reference is assigned to a static variable, the value of the second call to the & get_instance_ref () function is not remembered.
Source: http://php.net/manual/en/language.variables.scope.php
Calling a non-static method in static mode will result in an E_STRICT error.
Just like all other PHP static variables, static attributes can only be initialized as text or constants and cannot use expressions. Therefore, static attributes can be initialized as integers or arrays, but they cannot be initialized as another variable or function return value or point to an object.
Source: http://php.net/manual/zh/language.oop5.static.php
Therefore, global and static do not use reference assignment, which is determined by the php interpreter.