diff --git a/language/references.xml b/language/references.xml index 72fe8f7e40..459aec6499 100644 --- a/language/references.xml +++ b/language/references.xml @@ -21,8 +21,7 @@ -$a =& $b - + $a =& $b it means that $a and $b point to @@ -38,23 +37,27 @@ $a =& $b - The second thing references do is to pass variables by-references. This is + The second thing references do is to pass variables by-reference. This is done by making local function variable and caller variable to be reference to the same content. Example: -function foo(&$var) { - $var++; -} + function foo(&$var) { + $var++; + } -$a=5; -foo($a); - + $a=5; + foo($a); - will make $a to be 6. + will make $a to be 6. This happens because in the function foo the variable $var refers to the same content as $a. + + + The third thing reference can do is + return by-reference. + @@ -65,22 +68,22 @@ foo($a); -function foo(&$var) { - $var =& $GLOBALS["baz"]; -} -foo($bar); - + function foo(&$var) { + $var =& $GLOBALS["baz"]; + } + foo($bar); What will happen that $var in foo will be bound with $bar in caller, but then it will be re-bound with - $GLOBALS["baz"]. There's no way to bind $bar - in caller to something else using reference mechanism, since - $bar is not available in the function foo (it is - represented by $var, but $var - has only variable contents and not name-to-value binding). + $GLOBALS["baz"]. There's no way to bind + $bar in the caller to something else using reference + mechanism, since $bar is not available in the function + foo (it is represented by $var, but + $var has only variable contents and not name-to-value + binding in the calling symbol table). @@ -93,18 +96,25 @@ foo($bar); -function &find_var($param) { - ...code... - return $found_var; -} + function &find_var($param) { + ...code... + return $found_var; + } -$foo =& find_var($bar); - + $foo =& find_var($bar); + $foo->x = 2; + + In this example, property of the object returned by the + find_var function would be set, not of the copy, as + it would be without using reference syntax. - Unlike parameter passing, here you use & in both places. + Unlike parameter passing, here you have to use & + in both places - to indicate that you return by-reference, not a copy + as usual, and to indicate than reference binding and not usual + assignment should be done for $foo. @@ -118,19 +128,17 @@ $foo =& find_var($bar); -$a = 1; -$b =& $a; -unset($a); - + $a = 1; + $b =& $a; + unset($a); - won't unset $b, just $a. - Again, it might be useful to think about this as analogous to Unix unlink - call. + Again, it might be useful to think about this as analogous to Unix + unlink call. @@ -154,7 +162,7 @@ unset($a); -$var =& $GLOBALS["var"]; + $var =& $GLOBALS["var"];