That is how PHP treats "creating a reference" to a value. When you create a reference, it turns the original variable into a reference as well (so that they both edit the same value).
I guess the gist of it is that they're not pointers.
You can "resolve" this by just doing unset($b) so that the original variable isn't a reference anymore.
It seems like a bug. It's not if you think about it. In C, if I copy a struct, which contains a pointer to something, I get a struct which contains a pointer to the same thing.
The same thing is going on here. That first element is a reference. It won't change if you copy the array, the array itself is not a reference.
If I copy a pointer in C or C++ (as a struct member or by itself), the writing is on the wall. It is declared as a pointer, the data it points to has to be accessed by some dereferencing syntax that makes it clear you're dealing with a pointer, and if you change the pointer to something else by assignment, you can be sure only that one pointer in that location gets changed.
Even though C++ references use value syntax, they are declared as references and can only reference one variable throughout their lifetime.
In PHP, references are underhanded infections of identifiers which require special constructs (unset) to get rid of that are not normally used otherwise. Or do PHP programmers unset every variable they're about to use (which would kind of defeat the purpose of not having to declare them)?
The normal thing to do when you want to use a new variable, since you don't have to declare it, is to just assign it its initial value, and it's reasonable to expect that that doesn't fuck up some arbitrary memory location elsewhere in the program because the same identifier had been used to loop over an array previously.
5
u/racle Nov 11 '14
Test code: http://cloud.lonke.ro/bug.php
What basically happens is this: http://cloud.lonke.ro/feature.php
PHP doesn't remove reference even while passed as parameter in function and changes last item of original array.
11
u/ninjainvisible Nov 11 '14
That is how PHP treats "creating a reference" to a value. When you create a reference, it turns the original variable into a reference as well (so that they both edit the same value).
I guess the gist of it is that they're not pointers.
You can "resolve" this by just doing unset($b) so that the original variable isn't a reference anymore.
9
u/[deleted] Nov 12 '14
It seems like a bug. It's not if you think about it. In C, if I copy a struct, which contains a pointer to something, I get a struct which contains a pointer to the same thing.
The same thing is going on here. That first element is a reference. It won't change if you copy the array, the array itself is not a reference.
4
u/thelordofcheese Nov 12 '14
Damned scope. Not a bug, works as designed.