(26-Jan-2004 11:57)
A not so simple Workaround...but still doable...have fun
class My{
var $value;
function get1(&$ref){
$ref[] =& $this;
}
function get2(&$ref){
$ref =& $this;
}
function get3(&$ref){
$ref = $this;
}
}
$m = new My();
$m->value = 'foo';
$m->get1($ref=array());
$m1 =& $ref[0];
$m1->value = 'bar';
echo "n".'Works but is ugly...';
echo "n".' m:'. get_class($m) . '->value = '. $m->value;
echo "n".' m1:'. get_class($m1) . '->value = '. $m1->value;
echo "n".'Does not work because references are not pointers...';
$m->value = 'foo';
$m->get2($m2);
$m2->value = 'bar';
echo "n".' m:'. get_class($m) . '->value = '. $m->value;
echo "n".' m2:'. get_class($m2) . '->value = '. $m2->value;
$m->value = 'foo';
$m->get3($m3);
$m3->value = 'bar';
echo "n".'Does not work becuase it is set to a copy';
echo "n".' m:'. get_class($m) . '->value = '.$m->value;
echo "n".' m3:'. get_class($m3) . '->value = '. $m3->value;
As said above references are not pointers.
Following example shows a difference between pointers and references.
This Code
<?
$b = 1;
$a =& $b;
print("<pre>");
print("$a === $b: ".(($a === $b) ? "ok" : "failed")."n");
print("unsetting $a...n");
unset($a);
print("now $a is ".(isset($a) ? "set" : "unset")." and $b is ".(isset($b) ? "set" : "unset")."n");
print("</pre>");
$b = 1;
$a =& $b;
print("<pre>");
print("$a === $b: ".(($a === $b) ? "ok" : "failed")."n");
print("unsetting $b...n");
unset($b);
print("now $a is ".(isset($a) ? "set" : "unset")." and $b is ".(isset($b) ? "set" : "unset")."n");
print("</pre>");
?>
will produce this output:
---------
$a === $b: ok
unsetting $a...
now $a is unset and $b is set
$a === $b: ok
unsetting $b...
now $a is set and $b is unset
---------
So you see that $a and $b are identical ($a === $b -> true), but if one of both is unset, the other is not effected.
8/8 首页 上一页 6 7 8 |