有一段时间一直迷惑于php中引用的传递,后来查手册及c源程序,并反复测试,大致对引用传递在内存中的模式有了一定的了解,后来为了加深印象,写了个总结,应该不会有大的问题——当然这是在php4中,在以后的版本中可能会有变化。当时写总结的时候,想锻炼一下英语,因此就凑合了一篇。不过本人英语不好,也懒得翻译,反正当时想自己看得懂就行了。今天心血来潮,突然觉得还蛮有用的,于是在这里现丑了,请大家指正。那位看得懂的帮忙翻译一下吧,我没空了。 <?php
/*
filename: setgettest.php
comment on assignment by value and referrence
assuming $var is a varialbe, its handle(pointer) is named as *var,
and its content is named as @var
the memory area of @var is referred by *var, if the *var is the same,
then the memory areas are the same, so *var is just like a pointer.
1. when $var = $var1
@var copied from @var1, but in the different memory area,
new *var assigned by the system, pointing to the memory area holding @var
*var and *var1 are different
2. when $var =& $var1,
*var assigned by *var1, and the @var is not assigned or copied,
it is absolutely the same as @var1, and in the same memory area
both *var and *var1 pointing to the memory area, that means they are the same.
passing by referrence
3.
function set1(&$s){
$var =& $s;
}
set1($var1) results:
*var1 passing to the function, and *s is the same as *var1,
then *var is the same as *s, the result is that *var is the same as *var1
and all the contents are the same obviously.
4.
function set2(&$s){
$var = $s;
}
set2($var1) results:
*var1 passing to the function, and *s is the same as *var1,
but when $var = $s executes, from 1. we can see @var is the same as @s,
but *var is different from *s, so @var and @s is not in the same memory area,
while @s and @var1 is sharing the same memory area, also *var1 and *s are the same.
5.
normal function return:
function get(){ return $var1; }
assuming the result is referred by a variable $result.
then @result is copied from @var1 but *result is not the same as *var1
when $var = get();
first you get a variable $result, as i said above, @result is the same as @var1, but *result
is different from *var1, and next $var = $result executes.
as i said in 1., you can find, @var is the same as @result and the same as @var1,
but *var is different from *result and *var1;
while $var =& get() just means:
*var is the same as *result, so @var and @result are in the same memory area,
but they are still different from those of $var1,
both the memory area of @var1 and *var1,
6.
returning by referrence
function &get(){ return $var1; }
there are two ways to get the result
$var = get(); and $var =& get(); now i will tell the difference
i. $var = get();
the *result is the same as *var1 and so @result and @var1 are the same.
and then $var = $result executes,
*var is not the same as *result, and also different from *var1,
but their contents are the same.
i. $var =& get();
the *result is the same as *var1 and so @result and @var1 are the same.
and then $var =& $result executes,
this means $var and $result are the same, both of @ and *
*/
// the test is the following
function println($s = ""){
print "$s<br>/n";
}
class getsettest
{
var $var = null;
function setbyref(&$arg){
$this->var =& $arg;
}
function passbyref(&$arg){
$this->var = $arg;
}
function setbyval($arg){
$this->var = $arg;
}
function &getbyref(){
return $this->var;
}
function getbyval(){
return $this->var;
}
}
$o = new getsettest;
println("============ setbyref getbyref =============");
println("-----------------before change----------------");
$in = "before change";
$o->setbyref($in);
$outbyval = $o->getbyref();
$outbyref =& $o->getbyref();
println("/$in: ".$in);
println("/$outbyval: ".$outbyval);
println("/$outbyref: ".$outbyref);
println("/$this->var: ".$o->var);
println("-----------------after change-----------------");
$in = "after change";
println("/$in: ".$in);
println("/$outbyval: ".$outbyval);
println("/$outbyref: ".$outbyref);
println("/$this->var: ".$o->var);
println();
println("============ setbyref getbyval =============");
println("-----------------before change----------------");
$in = "before change";
$o->setbyref($in);
$outbyval = $o->getbyval();
$outbyref =& $o->getbyval();
println("/$in: ".$in);
println("/$outbyval: ".$outbyval);
println("/$outbyref: ".$outbyref);
println("/$this->var: ".$o->var);
println("-----------------after change-----------------");
$in = "after change";
println("/$in: ".$in);
println("/$outbyval: ".$outbyval);
println("/$outbyref: ".$outbyref);
println("/$this->var: ".$o->var);
println();
println("============ passbyref getbyval =============");
println("-----------------before change----------------");
$in = "before change";
$o->passbyref($in);
$outbyval = $o->getbyval();
$outbyref =& $o->getbyval();
println("/$in: ".$in);
println("/$outbyval: ".$outbyval);
println("/$outbyref: ".$outbyref);
println("/$this->var: ".$o->var);
println("-----------------after change-----------------");
$in = "after change";
println("/$in: ".$in);
println("/$outbyval: ".$outbyval);
println("/$outbyref: ".$outbyref);
println("/$this->var: ".$o->var);
println();
/*
以下输出结果是我(夜猫子)擅自编辑添加的,主要是为后来人查看方便加在这里,越肉代庖,向longnetpro致歉
输出结果:
============ setbyref getbyref =============
-----------------before change----------------
$in: before change
$outbyval: before change
$outbyref: before change
$this->var: before change
-----------------after change-----------------
$in: after change
$outbyval: before change
$outbyref: after change
$this->var: after change
============ setbyref getbyval =============
-----------------before change----------------
$in: before change
$outbyval: before change
$outbyref: before change
$this->var: before change
-----------------after change-----------------
$in: after change
$outbyval: before change
$outbyref: before change
$this->var: after change
============ passbyref getbyval =============
-----------------before change----------------
$in: before change
$outbyval: before change
$outbyref: before change
$this->var: before change
-----------------after change-----------------
$in: after change
$outbyval: before change
$outbyref: before change
$this->var: after change
*/
?>