what is a quote
quoting in php means accessing the same variable contents with different names. this is not like a c pointer; instead, the reference is a symbol table alias. note that in php, variable names and variable contents are different, so the same content can have different names. the closest analogy is unix's filenames and the files themselves - the variable names are the directory entries, and the variable contents are the files themselves. references can be thought of as hardlinks in unix file systems.
what does a quote do
php's references allow two variables to point to the same content. meaning, when doing this:
<?php $a =& $b; ?>
this means $a and $b point to the same variable.
note:
$a and $b are exactly the same here. it’s not that $a points to $b or vice versa, but that $a and $b point to the same place.
note:
if an array with a reference is copied, its value is not dereferenced. the same is true for passing array values to functions.
note:
if an undefined variable is assigned by reference, passed by reference, or returned by reference, the variable will be automatically created.
example #1 using references to undefined variables
<?php function foo(&$var) { } foo($a); // $a is "created" and assigned to null $b = array(); foo($b['b']); var_dump(array_key_exists('b', $b)); // bool(true) $c = new stdclass; foo($c->d); var_dump(property_exists($c, 'd')); // bool(true) ?>
the same syntax can be used in functions, which return references, and in the new operator (php 4.0.4 and later):
<?php $bar =& new fooclass(); $foo =& find_var($bar); ?>
as of php 5, new automatically returns a reference, so using =& here is obsolete and produces an e_strict level message.
note:
not using the & operator causes a copy of the object to be made. if you use $this in a class, it will apply to the current instance of that class. assignment without & will copy the instance (e.g. object) and $this will act on the copy, which is not always the desired result. due to performance and memory consumption issues, you usually only want to work on one instance.
although it is possible to suppress any error messages in a constructor using the @ operator, such as @new, this has no effect when using the &new statement. this is a limitation of the zend engine and will cause a parsing error.
warning
if a reference is assigned to a variable declared global within a function, the reference is only visible inside the function. this can be avoided by using the $globals array.
example #2 reference global variables within functions
<?php $var1 = "example variable"; $var2 = ""; function global_references($use_globals) { global $var1, $var2; if (!$use_globals) { $var2 =& $var1; // visible only inside the function } else { $globals["var2"] =& $var1; // visible also in global context } } global_references(false); echo "var2 is set to '$var2'\n"; // var2 is set to '' global_references(true); echo "var2 is set to '$var2'\n"; // var2 is set to 'example variable' ?>
think of global $var; as shorthand for $var =& $globals['var'];. thus assigning another reference to $var only changes the reference to the local variable.
note:
if a variable with a reference is assigned a value in a foreach statement, the referenced object is also changed.
example #3 quotation and foreach statement
<?php $ref = 0; $row =& $ref; foreach (array(1, 2, 3) as $row) { // do something } echo $ref; // 3 - last element of the iterated array ?>
the second thing a reference does is pass a variable by reference. this is accomplished by creating a local variable within the function and that variable references the same content in the calling scope. for example:
<?php function foo(&$var) { $var++; } $a=5; foo($a); ?>
will change $a to 6. this is because in function foo the variable $var points to the same thing that $a points to. see passing by reference for a more detailed explanation.
the third thing that references do is reference returns.
what a quote is not
as mentioned before, references are not pointers. this means that the following construct will not produce the expected effect:
<?php function foo(&$var) { $var =& $globals["baz"]; } foo($bar); ?>
this will cause the $var variable in the foo function to be bound to $bar when the function is called, but then re-bound to $globals["baz"]. it is not possible to bind $bar to another variable within the function call scope through the reference mechanism, because there is no variable $bar in function foo (it is represented as $var, but $var only has the variable content and no call symbol table name-to-value binding). you can use reference returns to reference variables selected by the function.
pass by reference
you can pass a variable by reference to a function so that the function can modify the value of its argument. the syntax is as follows:
<?php function foo(&$var) { $var++; } $a=5; foo($a); // $a is 6 here ?>
note that there are no reference symbols in the function call - only in the function definition. the function definition alone is enough for parameters to be passed correctly by reference. in recent versions of php, if you use & in foo(&$a); you will get a warning that "call-time pass-by-reference" is deprecated.
the following can be passed by reference:
variables, such as foo($a)
new statement, such as foo(new foobar())
a reference returned from a function, for example:
<?php function &bar() { $a = 5; return $a; } foo(bar()); ?>
for detailed explanation, see reference return.
any other expression cannot be passed by reference, and the result is undefined. for example, the following example of passing by reference is invalid:
<?php function bar() // note the missing & { $a = 5; return $a; } foo(bar()); // 自 php 5.0.5 起导致致命错误 foo($a = 5) // 表达式,不是变量 foo(5) // 导致致命错误 ?>
these conditions are available in php 4.0.4 and later versions.
return by reference
reference return is used when you want to use a function to find which variable the reference should be bound to. don't use return references to increase performance, the engine is smart enough to optimize it itself. only return references if there is a valid technical reason! to return a reference, use this syntax:
<?php class foo { public $value = 42; public function &getvalue() { return $this->value; } } $obj = new foo; $myvalue = &$obj->getvalue(); // $myvalue is a reference to $obj->value, which is 42. $obj->value = 2; echo $myvalue; // prints the new value of $obj->value, i.e. 2. ?>
in this example, the properties of the object returned by the getvalue function will be assigned values, not copied, just as if reference syntax was not used.
note: unlike parameter passing, the & symbol must be used in both places here - indicating that a reference is returned, not the usual copy, and also indicating that $myvalue is bound as a reference, not usual assignment.
note: if you try to return a reference from a function like this: return ($this->value);, this will not work because you are trying to return the result of an expression rather than a referenced variable. you can only return reference variables from functions - there is no other way. if code attempts to return the result of a dynamic expression or the new operator, an e_notice error is issued starting with php 4.4.0 and php 5.1.0.
<?php function &test(){ static $b=0;//申明一个静态变量 $b=$b+1; echo $b; return $b; } $a=test();//这条语句会输出$b的值为1 $a=5; $a=test();//这条语句会输出$b的值为2 $a=&test();//这条语句会输出$b的值为3 $a=5; $a=test();//这条语句会输出$b的值为6 ?>
calling a function using $a=test() only assigns the value of the function to $a. any changes to $a will not affect $b in the function. however, through $a=&test( ) method, its function is to point the memory address of the $b variable in return $b and the memory address of the $a variable to the same place, which produces the equivalent of this effect ($a=&b;) so changing the value of $a also changes the value of $b, so after executing $a=&test(); $a=5;, the value of $b becomes 5.
dereference
when you unset a reference, you just break the binding between the variable name and the variable's contents. this does not mean that the variable contents are destroyed. for example:
<?php $a = 1; $b =& $a; unset($a); ?>
will not unset $b, just $a.
it may be helpful to compare this with the unix unlink call.
reference positioning
many php syntax constructs are implemented through the reference mechanism, so everything mentioned above about reference binding applies to these constructs as well. some constructs, such as pass-by-reference and return, have already been mentioned above. other structures that use references are:
global reference
when you declare a variable with global $var you actually create a reference to the global variable. in other words, it is the same as doing this:
<?php $var =& $globals["var"]; ?>
this means that, for example, unset $var will not unset a global variable.
the results of using unset($a) and $a=null are different. if the block of memory has only one mapping of $a, then unset($a) is equivalent to $a=null. the reference count of the memory becomes 0 and it is automatically recycled; if the block of memory has two mappings of $a and $b , then unset($a) will cause $a=null and $b remains unchanged, and $a=null will cause $a=$b=null.
reason: assigning a variable to null will cause the reference count of the memory block corresponding to the variable to be directly set to 0 and automatically recycled.
$this
in an object method, $this is always a reference to the object that calls it.
the role of quotations
if the program is relatively large, there are many variables referencing the same object, and you want to manually clear the object after using it, i personally recommend using the "&" method, and then using $var=null to clear it. otherwise, use the default of php5 method. in addition, for the transfer of large arrays in php5, it is recommended to use the "&" method, after all, it saves memory space.
let’s have another little episode. the pointing (similar to pointer) function of the address in php is not implemented by the user himself, but is implemented by the zend core. the reference in php adopts the principle of "copy-on-write", that is, unless when a write operation occurs, variables or objects pointing to the same address will not be copied.
in layman terms
1: if there is the following code
<?ph $a="ABC"; $b=$a; ?>
in fact, at this time, $a and $b both point to the same memory address, rather than $a and $b occupying different memories.
2: if you add the following code to the above code
$a="efg";
since the data in the memory pointed to by $a and $b needs to be rewritten, the zend core will automatically determine at this time to automatically produce a data copy of $a for $b and re-apply for a piece of memory for storage.