网站导航免费论文 原创论文 论文搜索 原创论文 网学软件 学术大家 资料中心 会员中心 问题解答 原创论文 论文素材 设计下载 最新论文 下载排行 论文上传 在线投稿 联系我们
返回网学首页
网学联系
最新论文 推荐专题 热门论文 素材专题
当前位置: 网学 > 编程文档 > PHP > 正文
为您详解PHP开发工具的使用与分析
来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/12/05
下载{$ArticleTitle}原创论文样式
  有一段时间一直迷惑于PHP中引用的传递,后来查手册及C源程序,并反复测试,大致对引用传递在内存中的模式有了一定的了解,后来为了加深印象,写了个总结,应该不会有大的问题——当然这是在PHP4中,在以后的版本中可能会有变化。当时写总结的时候,想锻炼一下英语,因此就凑合了一篇。不过本人英语不好,也懒得翻译,反正当时想自己看得懂就行了。今天心血来潮,突然觉得还蛮有用的,于是在这里现丑了,请大家指正。那位看得懂的帮忙翻译一下吧,我没空了。 <?php 
/* 
filename:SetGetTest.php 
commentonassignmentbyvalueandreferrence 
assuming$varisavarialbe,itshandle(pointer)isnamedas*var, 
anditscontentisnamedas@var 
thememoryareaof@varisreferredby*var,ifthe*varisthesame, 
thenthememoryareasarethesame,so*varisjustlikeapointer. 
1.when$var=$var1 
var1,butinthedifferentmemoryarea, 
new*varassignedbythesystem,pointingtothememoryareaholding@var 
*varand*var1aredifferent 
2.when$var=&$var1, 
*varassignedby*var1,andthe@varisnotassignedorcopied, 
itisabsolutelythesameas@var1,andinthesamememoryarea 
both*varand*var1pointingtothememoryarea,thatmeanstheyarethesame. 
passingbyreferrence 
3. 
functionset1(&$s){ 
$var=&$s; 
} 
set1($var1)results: 
*var1passingtothefunction,and*sisthesameas*var1, 
then*varisthesameas*s,theresultisthat*varisthesameas*var1 
andallthecontentsarethesameobviously. 
4. 
functionset2(&$s){ 
$var=$s; 
} 
set2($var1)results: 
*var1passingtothefunction,and*sisthesameas*var1, 
butwhen$var=$sexecutes,from1.wecansees, 
but*varisdifferentfrom*s,sosisnotinthesamememoryarea, 
whilevar1issharingthesamememoryarea,also*var1and*sarethesame. 
5. 
normalfunctionreturn: 
functionget(){return$var1;} 
assumingtheresultisreferredbyavariable$result. 
thenvar1but*resultisnotthesameas*var1 
when$var=get(); 
firstyougetavariable$result,asIsaidabove,var1,but*result 
isdifferentfrom*var1,andnext$var=$resultexecutes. 
AsIsaidin1.,youcanfind,resultandthesameas@var1, 
but*varisdifferentfrom*resultAND*var1; 
while$var=&get()justmeans: 
*varisthesameas*result,soresultareinthesamememoryarea, 
buttheyarestilldifferentfromthoseof$var1, 
boththememoryareaof@var1and*var1, 
6. 
returningbyreferrence 
function&get(){return$var1;} 
therearetwowaystogettheresult 
$var=get();and$var=&get();nowIwilltellthedifference 
I.$var=get(); 
the*resultisthesameas*var1andsovar1arethesame. 
andthen$var=$resultexecutes, 
*varisnotthesameas*result,andalsodifferentfrom*var1, 
buttheircontentsarethesame. 
I.$var=&get(); 
the*resultisthesameas*var1andsovar1arethesame. 
andthen$var=&$resultexecutes, 
thismeans$varand$resultarethesame,bothof@and* 
*/ 
//thetestisthefollowing 
functionprintln($s=""){ 
print"$s<br>"; 
} 
classGetSetTest 
{ 
var$var=null; 
functionsetByRef(&$arg){ 
$this->var=&$arg; 
} 
functionpassByRef(&$arg){ 
$this->var=$arg; 
} 
functionsetByVal($arg){ 
$this->var=$arg; 
} 
function&getByRef(){ 
return$this->var; 
} 
functiongetByVal(){ 
return$this->var; 
} 
} 
$o=newGetSetTest; 
println("============setByRefgetByRef============="); 
println("-----------------Beforechange----------------"); 
$in="beforechange"; 
$o->setByRef($in); 
$outByVal=$o->getByRef(); 
$outByRef=&$o->getByRef(); 
println("$in:".$in); 
println("$outByVal:".$outByVal); 
println("$outByRef:".$outByRef); 
println("$this->var:".$o->var); 
println("-----------------Afterchange-----------------"); 
$in="afterchange"; 
println("$in:".$in); 
println("$outByVal:".$outByVal); 
println("$outByRef:".$outByRef); 
println("$this->var:".$o->var); 
println(); 
println("============setByRefgetByVal============="); 
println("-----------------Beforechange----------------"); 
$in="beforechange"; 
$o->setByRef($in); 
$outByVal=$o->getByVal(); 
$outByRef=&$o->getByVal(); 
println("$in:".$in); 
println("$outByVal:".$outByVal); 
println("$outByRef:".$outByRef); 
println("$this->var:".$o->var); 
println("-----------------Afterchange-----------------"); 
$in="afterchange"; 
println("$in:".$in); 
println("$outByVal:".$outByVal); 
println("$outByRef:".$outByRef); 
println("$this->var:".$o->var); 
println(); 
println("============passByRefgetByVal============="); 
println("-----------------Beforechange----------------"); 
$in="beforechange"; 
$o->passByRef($in); 
$outByVal=$o->getByVal(); 
$outByRef=&$o->getByVal(); 
println("$in:".$in); 
println("$outByVal:".$outByVal); 
println("$outByRef:".$outByRef); 
println("$this->var:".$o->var); 
println("-----------------Afterchange-----------------"); 
$in="afterchange"; 
println("$in:".$in); 
println("$outByVal:".$outByVal); 
println("$outByRef:".$outByRef); 
println("$this->var:".$o->var); 
println(); 
/* 
以下输出结果是我(夜猫子)擅自编辑添加的,主要是为后来人查看方便加在这里,越肉代庖,向longnetpro致歉 
输出结果: 
============setByRefgetByRef============= 
-----------------Beforechange---------------- 
$in:beforechange 
$outByVal:beforechange 
$outByRef:beforechange 
$this->var:beforechange 
-----------------Afterchange----------------- 
$in:afterchange 
$outByVal:beforechange 
$outByRef:afterchange 
$this->var:afterchange 
============setByRefgetByVal============= 
-----------------Beforechange---------------- 
$in:beforechange 
$outByVal:beforechange 
$outByRef:beforechange 
$this->var:beforechange 
-----------------Afterchange----------------- 
$in:afterchange 
$outByVal:beforechange 
$outByRef:beforechange 
$this->var:afterchange 
============passByRefgetByVal============= 
-----------------Beforechange---------------- 
$in:beforechange 
$outByVal:beforechange 
$outByRef:beforechange 
$this->var:beforechange 
-----------------Afterchange----------------- 
$in:afterchange 
$outByVal:beforechange 
$outByRef:beforechange 
$this->var:afterchange 
*/ 
?>

(责任编辑:admin)

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号