찾다
php教程php手册为您详解PHP开发工具的使用与分析

  有一段时间一直迷惑于PHP中引用的传递,后来查手册及C源程序,并反复测试,大致对引用传递在内存中的模式有了一定的了解,后来为了加深印象,写了个总结,应该不会有大的问题——当然这是在PHP4中,在以后的版本中可能会有变化。当时写总结的时候,想锻炼一下英语,因此就凑合了一篇。不过本人英语不好,也懒得翻译,反正当时想自己看得懂就行了。今天心血来潮,突然觉得还蛮有用的,于是在这里现丑了,请大家指正。那位看得懂的帮忙翻译一下吧,我没空了。

/* 

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
\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 
*/ 

?>



성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

mPDF

mPDF

mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

맨티스BT

맨티스BT

Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기