Difference between revisions of "PHP/referencing"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< PHP
Jump to navigation Jump to search
(Created page with "In PHP, a variable can be accessed either by ''value'' or by ''reference''. All PHP variables are references; the question is what you're accessing – the location of the...")
 
Line 36: Line 36:
 
* [https://www.php.net/manual/en/language.operators.assignment.php#language.operators.assignment.reference Assignment Operators: Assignment by Reference]
 
* [https://www.php.net/manual/en/language.operators.assignment.php#language.operators.assignment.reference Assignment Operators: Assignment by Reference]
 
* [https://www.php.net/manual/en/functions.arguments.php#functions.arguments.by-reference Function arguments: Passing arguments by reference]
 
* [https://www.php.net/manual/en/functions.arguments.php#functions.arguments.by-reference Function arguments: Passing arguments by reference]
 +
* [https://www.php.net/manual/en/functions.returning-values.php Returning values]: see "Example #3 Returning a reference from a function"
 
* [https://www.php.net/manual/en/language.oop5.references.php Objects and references]: they're not handled quite the same
 
* [https://www.php.net/manual/en/language.oop5.references.php Objects and references]: they're not handled quite the same
 
** short explanation: objects are looked up in a different table from references
 
** short explanation: objects are looked up in a different table from references

Revision as of 18:54, 27 July 2021

In PHP, a variable can be accessed either by value or by reference. All PHP variables are references; the question is what you're accessing – the location of the value or what's stored there. By default, you're accessing the value; referencing syntax allows you to refer to the location instead.

Concepts

Each variable in PHP is a reference to a storage location; the details of this are generally hidden. When one variable is assigned a reference to another (rather than just the value) they then both refer to the same storage location. This storage location will continue to exist as long as at least one variable is using it.

References do allow some of the functionality provided by pointers in other languages (like C/C++), but unlike pointers they are de-referenced by default. A better analogy (the PHP manual agrees) is to think of them like hard file links in Unix. Conceptually, $b = &$a means "$b and $a now point to the same content", not "$b is a pointer to $a's content".

Basics

To return a reference instead of a value, use the & operator when referring to the source variable:

$vtest = 'test value';
$vref = &$vtest;
echo "VTEST: ";
var_dump($vtest);
echo '<br>';
echo "VREF: ";
var_dump($vref);
echo '<br>';
$vref = 'another test';
echo "VTEST: ";
var_dump($vtest);
echo '<br>';

Output:

VTEST: string(10) "test value"
VREF: string(10) "test value"
VTEST: string(12) "another test"

This illustrates some key points:

  • The var_dump() function can't tell the difference between a variable that was given a reference and one that was given a value.
    • ...because there isn't one, actually.
  • Writing a value to a variable that contains a reference overwrites the value of the reference target, rather than replacing the reference.
    • This is kind of essential to how references work: spooky action at a distance, basically.
    • This also implies that the only way to change a reference-holding variable directly is to assign it a new reference or unset() it.

Links