-
Notifications
You must be signed in to change notification settings - Fork 735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Outdated or Confusing Information on What References Are Not #2740
Comments
The point is that trying to assign by reference a variable which is out of scope will not work. The example could probably be expanded to have all the relevant information, as the result of this example should make clear what is implied here: https://3v4l.org/o5MHR |
There is no such thing as a reference to a variable. That's not how they work. Check the PHP Internals Book for more information. It's not exhaustive on the matter, but the most important point there is what it says at the very beginning of the "Reference semantics" section:
A "reference" is simply when the "common value" is being shared by multiple variables at once. So regarding the documentation's example code,
If the assignment was a normal assignment instead, the common value shared by both // normal assignment: copy the value's data to another value
$var->value->data = $GLOBALS["baz"]->value->data;
// $var->value and $baz->value are two different things, but now they have copies of the same data
// by-ref assignment: share the entire value with another variable
$var->value = $GLOBALS["baz"]->value;
// now $var->value and $baz->value are actually the same thing |
Thanks a lot for your answers. I think I got what references are in PHP in general. I should have been more precise. I did not mean a reference to a variable, but to a value shared by two variables. |
From manual page: https://php.net/language.references.arent
The information on the mentioned page seems outdated or at least very confusing:
You coud just use
$GLOBALS["bar"] =& ...
Unless I am misunderstanding something, for which I would apoligise, the problem of$bar
not being available would only be the case if the defined function would be itself in another function scope. I am also unsure what is meant byas it is a reference, first to
$bar
and then to global$baz
. This part is probably meant to make the distinction between references and c pointers clear?The text was updated successfully, but these errors were encountered: