Describes the use and analysis of PHP Development tools

Source: Internet
Author: User
For a while, I have been confused with the reference transfer in PHP. later I checked the manual and C source program and tested them repeatedly. I had a certain understanding of the mode in which the reference is passed in the memory, later, I wrote a summary to deepen my impression, so there should be no major problems-of course, this is in PHP4, and in later versions, I have been confused about the reference transfer in PHP for a period of time, later, I checked the manual and the C source program and tested them repeatedly. I had a certain understanding of the mode in which the references are passed in the memory. later I wrote a summary to help me better understand the concept, there should be no major issues-of course, this is in PHP4 and may change in later versions. At that time, when I wrote a summary, I wanted to exercise my English, so I made up an article. However, I am not good at English and I am too lazy to translate it. I just wanted to understand it myself. Today, I suddenly thought it was quite useful, so I am ugly here. please correct me. I am not busy.

The following is a reference clip:
/*

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 obviusly.

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 ();

/*
The following output is edited and added by me (Night Owl) without authorization, mainly for the convenience of viewing by later users. Yuexiao apologized to longnetpro
Output result:
============= 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
*/

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.