Question about global and static variable reference in PHP?

Source: Internet
Author: User
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 PHPerI 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 beginnerPHPerI 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.