PHP 7.1.21 Released

Qu'est ce qu'une référence ?

En PHP, les références sont un moyen d'accéder au contenu d'une même variable en utilisant plusieurs noms. Les références ne sont pas comme des pointeurs en C : vous ne pouvez pas effectuer d'opération arithmétique de pointeurs sur celles-ci, ce ne sont pas des adresses mémoires, etc. Vous pouvez consulter Ce que les références ne sont pas pour plus d'informations. En fait, les références sont des alias dans la table des symboles. Notez qu'en PHP, le nom d'une variable et son contenu sont deux notions distinctes, ce qui fait que l'on peut donner plusieurs noms au même contenu. On peut faire l'analogie avec les fichiers sous Unix, et leur noms : les noms des variables sont les entrées dans un répertoire, tandis que le contenu de la variable est le fichier en lui-même. Les références en PHP peuvent alors être considérées comme semblables aux liens sous Unix.

add a note add a note

User Contributed Notes 4 notes

up
44
273118949 at qq dot com
10 months ago
it just likes a person who has two different names.
up
0
Anonymous
24 days ago
Unlike in C, PHP references are not treated as pre-dereferenced pointers, but as complete aliases.

The data that they are aliasing ("referencing") will not become available for garbage collection until all references to it have been removed.

"Regular" variables are themselves considered references, and are not treated differently from variables assigned using =& for the purposes of garbage collection.

The following examples are provided for clarification.

1) When treated as a variable containing a value, references behave as expected. However, they are in fact objects that *reference* the original data.

<?php
var = "foo";
$ref1 =& $var; // new object that references $var
$ref2 =& $ref1; // references $var directly, not $ref1!!!!!

echo $ref; // >foo

unset($ref);

echo
$ref1; // >Notice:  Undefined variable: ref1
echo $ref2; // >foo
echo $var; // >foo
?>

2) When accessed via reference, the original data will not be removed until *all* references to it have been removed. This includes both references and "regular" variables assigned without the & operator, and there are no distinctions made between the two for the purpose of garbage collection.

<?php
$var
= "foo";
$ref =& $var;

unset(
$var);

echo
$var; // >Notice:  Undefined variable: var
echo $ref; // >foo
?>

3) To remove the original data without removing all references to it, simply set it to null.

<?php
$var
= "foo";
$ref =& $var;

$ref = NULL;

echo
$var; // Value is NULL, so nothing prints
echo $ref; // Value is NULL, so nothing prints
?>

4) Placing data in an array also counts as adding one more reference to it, for the purposes of garbage collection.

For more info, see http://php.net/manual/en/features.gc.refcounting-basics.php
up
-8
anon
1 year ago
In summary, "&$reference" means "do-not-copy-on-write the value here, in perpetuity". Assigning by reference is not assignment, it's "make &$variable a reference and its value do-not-copy-on-write, in perpetuity, and make the variable I'm assigning to use that do-not-copy-on-write value as well".

To "unreference/unalias" you have to either unset or make an explicit copy into a new variable.

Object properties that are references will survive cloning and remain references. Generally the same is true with references in arrays and PHP's array functions (combine, intersect, call_user_func, func_get_args, etc).

Calling a function that uses a reference parameter will *make* the supplied variable a reference. This is also true when using variadic array expansion for arguments; the supplier's array element will become a reference.

Generally, don't use them unless you're dealing with low-level calls, or need an accumulator, etc. For poorly designed functions that use them, give them a copy to mangle.
up
-38
stilezy
2 years ago
One subtle effect of PHP's assign-by-reference is that operators which might be expected to work with args that are references usually don't.  For example:

$a = ($b ? &$c : &$d);

fails (parser error) but the logically identical

if ($b)
   $a =& $c;
else
   $a =& $cd;

works. It's not always obvious why seemingly identical code throws an error in the first case. This is discussed on a PHP bug report ( https://bugs.php.net/bug.php?id=54740 ). TL;DR version, it acts more like an assignment term ($var1) "=&" ($var2) than a function/operator ($var1) "=" (&$var2).
To Top