Gönderim Nedir?

PHP'de gönderimler aynı değişken içeriğine farklı isimlerle erişmek demektir. C'deki göstericilere benzemezler; örneğin, üzerlerinde gösterici aritmetiği kullanamazsınız, çünkü gerçekte bellek adresleri değillerdir.Ayrıntılar için Gönderimler Ne Değildir? bölümüne bakınız. daha çok simge tablosu PHP'de şuna dikkat edin: Değişken içeriği ve değişken ismi farklı şeylerdir. Bu bakımdan, aynı içeriğin farklı isimleri olabilir. En yakın benzerlik, Unix dosya isimleri ve dosyaları ile kurulabilir; değişken isimleri dizinlerse, değişken içeriği de dizinin içindeki dosyadır. Gönderimler ise Unix dosya sistemindeki sabit bağlar olarak düşünülebilir.

add a note add a note

User Contributed Notes 4 notes

273118949 at qq dot com
11 months ago
it just likes a person who has two different names.
1 month 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.

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

echo $ref; // >foo


$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.

= "foo";
$ref =& $var;


$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.

= "foo";
$ref =& $var;

$ref = NULL;

$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
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.
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;
   $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