|
|
References are a means in PHP to access the same variable content
by different names. They are not like C pointers, they are symbol
table aliases. Note that in PHP, variable name and variable content
are different, so the same content can have different names.
The most close analogy is with Unix filenames and files -
variable names are directory entries, while variable contents is
the file itself. References can be thought of as hardlinking in
Unix filesystem.
User Contributed Notes References Explained |
|
20-Dec-2001 04:46 |
|
Succesive assignement by reference and simple assignement :
$a = 'old';
$b = &$a;
$a = 'new';// now $b's value is new
$c = $b;// simple copy
$a = 'new new'; // $b is 'new new', $c is still 'new'
And now with arrays :
$a[0] = 'old';
$b = &$a;
$c = $b;//simple copy
$a[0] = 'new';// $b[0] is new, and $c[0] is still 'old'
BUT :
$a = 'old';
$b[0] = &$a;
$c = $b; // simple copy
$a = 'new';//now BOTH $b[0] and $c[0] are 'new'
A simple copying of an array (or an object) does NOT
break references for the values (or instance variables).
Use var_dump() to see which of the values in an array
are references.
Ivan
|
|
11-Jan-2002 01:44 |
|
You may be looking for how to iterate over an array by reference. For
example, you have an array of objects, and you want to call methods that
modify the objects. A normal foreach doesn't work, as the elements of the
array are copied. each() doesn't work either. You have to use a for loop.
Example of what *COPIES* objects (not what you probably want):
foreach($object_list as $object) {
$object->method();
}
Example of what does not copy objects (probably what you want):
for ($c = 0; $c = count($object_list); $c++) {
$object = &$object_list[$c];
$object->method();
}
You get the picture.
|
|
17-Jan-2002 04:55 |
|
Caveat, if your using an associative array with the key pointing to a
reference your going to want to do somehting like this:
reset($MyArray);
while($Key = key($MyArray))
{
$_MyRef = & $MyArray[$Key];
...Do Smothing...
next($MyArray);
}
|
|
cody at iensemble d0t c0m
07-Mar-2002 05:26 |
|
Another way to iterate over an array of references:
foreach (array_keys($myArray) as $key) {
doSomething($myArray[$key]);
}
This works on "normal" and associative arrays.
|
|
27-Mar-2002 10:37 |
|
For the Java programmers among you, there's a big difference between Java
and PHP. In Java, all objects are passed by value (as in PHP). However,
try this in PHP:
class Object {
var $value;
function Object() {
$this->value = 5;
}
function getValue() {
return $this->value;
}
function setValue($value) {
$this->value = $value;
}
}
function updateObject($object) {
$object->setValue($object->getValue() + 5);
echo ', ', $object->getValue();
}
$object = new Object();
echo $object->getValue();
updateObject($object);
echo ', ', $object->getValue();
It prints: '5, 10, 5'.
If you implement this same code in Java however, it will print '5, 10,
10'!
Explanation: 'passing by value' in Java means that assigning a different
value to a variable inside a function will not change it outside a
function. (PHP equivalent: '$object = new Object'.) But calling methods on
the variable (when it's an object) can change the object (In PHP:
'$object->setValue(9)'). With PHP the latter isn't true, unfortunately.
If you use objects in PHP, you have to use references almost all the time,
even if you don't want to assign a new value to an outside variable, but
just want to call methods (that might update the object).
|
|
02-Apr-2002 07:34 |
|
If you want to check whether there is a reference between two variables you
can use this function.
function check_reference(&$var1, &$var2)
{
// Save old values
$var1_old = $var1;
$var2_old = $var2;
// ($var1 . ".") must not be $var2
if($var1 . "." == $var2)
$var1 .= ".";
// Change $var1
$var1 .= ".";
// Did $var2 also change?
if($var2 == $var1)
$reference = true;
else
$reference = false;
// Old values
$var1 = $var1_old;
$var2 = $var2_old;
return $reference;
}
|
|
08-Apr-2002 09:34 |
|
A have found interesting strange in references to classes.
class Child{
function Child(&$parent)
{
$this->parent=&$parent;
$this->parent->var=10;
}
function doit()
{
$this->parent->var=20;
}
}
class A{
function A()
{
$this->child=new Child($this);
}
function doit()
{
$this->child->doit();
echo $this->var;
}
}
$a = new A;
$a->doit();
-----------------
In result we have 10, not 20.
And, if we call $a->doit(); from constructor �():
function A()
{
$this->child=new Child($this);
$this->doit();
}
we resulting 20.
The way to takearound this is make empty constructor, move all from it to
function constr(), and run some like:
$a=new A;
$a->me=&$a;
$a->constr();
$a->doit();
in constr() we have $this->child=new Child($this->me); [not
Child($this)].
and, as result we have 20 as would be expected.
That's this? Am I missunderstand something or it is PHP bug?
|
|
23-Apr-2002 12:07 |
|
When you do
$a = new A();
PHP creates an object for you, and returns a COPY of that object
So (if I get it right... ) the child is actually altering the original
object and not the copy that you stored in $a.
However if you do
$a =& new A();
PHP still creates an object, but returns a REFERENCE to that object.
The child is still altering the original - but you are happy, because you
have got a reference the original!
so if you change the line
$a = new A();
to
$a =& new A();
you'll get the result you expected.
(BTW wouldn't it be more correct to say that when you do:
$a = new A()
PHP returns not a copy but a reference to a copy?)
|
|
|
| |