diff --git a/language/variables.xml b/language/variables.xml index 5288ebd316..90d8cd8a83 100644 --- a/language/variables.xml +++ b/language/variables.xml @@ -1,5 +1,5 @@ - + Variables @@ -453,6 +453,111 @@ function Test() + + The Zend Engine 1, driving PHP4, implements the + static and global modifier for + variables in terms of references. For example, a true global variable + imported inside a function scope with the global + statement actually creates a reference to the global variable. This can + lead to unexpected behaviour which the following example addresses: + + + + + + + + + + Executing this example will result in the following output: + + + +NULL +object(stdClass)(0) { +} + + + + A similar behaviour applies to the static statement. + References are not stored statically: + + + + +property++; + return $obj; +} + +function &get_instance_noref() { + static $obj; + + echo "Static object: "; + var_dump($obj); + if (!isset($obj)) { + // Assign the object to the static variable + $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(); +]]> + + + + + Executing this example will result in the following output: + + + +Static object: NULL +Static object: NULL + +Static object: NULL +Static object: object(stdClass)(1) { + ["property"]=> + int(1) +} + + + + This example demonstrates that when assigning a reference to a static + variable, it's not remembered when you call the + &get_instance_ref() function a second time. + + +