Which great God can explain this example in the official PHP manual from a memory point of view, I am a beginner
PHPerDo not understand, please the elders of the liberal enlighten, the younger generation greatly thanks!
It is best to be able to run the following code 内存 under the scenario description.
Similar behavior applies to static statements as well. References are not stored statically:
function &get_instance_ref() { static $obj; echo 'Static object: '; var_dump($obj); if (!isset($obj)) { // 将一个引用赋值给静态变量 $obj = &new stdclass; } $obj->property++; return $obj;}function &get_instance_noref() { static $obj; echo 'Static object: '; var_dump($obj); if (!isset($obj)) { // 将一个对象赋值给静态变量 $obj = new stdclass; } $obj->property++; return $obj;}$obj1 = get_instance_ref();$still_obj1 = get_instance_ref();echo "\n";$obj2 = get_instance_noref();$still_obj2 = get_instance_noref();
The above routines will output:
Static object: NULLStatic object: NULLStatic object: NULLStatic object: object(stdClass)(1) {["property"]=>int(1)}
The example above demonstrates 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.
The source of this example: http://php.net/manual/en/language.variables.scope.php
Reply content:
Which big God can from the memory angle to explain the PHP Official Handbook of this example, I am a beginner PHPer do not quite understand, please the elders of the liberal enlighten, the juniors greatly thanks!
It is best to be able to run the following code 内存 under the scenario description.
Similar behavior applies to static statements as well. References are not stored statically:
function &get_instance_ref() { static $obj; echo 'Static object: '; var_dump($obj); if (!isset($obj)) { // 将一个引用赋值给静态变量 $obj = &new stdclass; } $obj->property++; return $obj;}function &get_instance_noref() { static $obj; echo 'Static object: '; var_dump($obj); if (!isset($obj)) { // 将一个对象赋值给静态变量 $obj = new stdclass; } $obj->property++; return $obj;}$obj1 = get_instance_ref();$still_obj1 = get_instance_ref();echo "\n";$obj2 = get_instance_noref();$still_obj2 = get_instance_noref();
The above routines will output:
Static object: NULLStatic object: NULLStatic object: NULLStatic object: object(stdClass)(1) {["property"]=>int(1)}
The example above demonstrates 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.
The source of this example: http://php.net/manual/en/language.variables.scope.php
Invoking a non-static method in a static manner results in an e_strict level error.
Just like all other PHP static variables, static properties can only be initialized to literals or constants, and expressions cannot be used. You can initialize a static property to an integer or an array, but you cannot initialize it to another variable or function return value, or point to an object.
Source: http://php.net/manual/zh/language.oop5.static.php
So global and static do not use reference assignment, which is determined by the PHP interpreter.