A period of time, I have been confused in PHP, and then check the manual and C source procedures, and repeatedly test it. It has a certain understanding of the reference delivery in memory. Later, in order to deepen the impression, write a summary, should There is no big problem - of course this is in PHP4, and there may be changes in future versions. When you write a summary, you want to exercise English, so it will make a piece. However, my English is not good, too lazy to translate, anyway, I think I can understand myself. Today, I'm really useful, so I am now ugly here, please correct it. That kind, I know, I am not empty.
[code: 1: 2433d9E506]
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 membrate area of @var is refered by * var, if the * var is the Same,
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 isa Holding @var
* Var and * var1 area 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
.
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 = $ results 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 is 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.
AND THEN $ VAR = $ RESULT EXECUTES,
* Var is not the same as * result, and also diffrent 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.
AND THEN $ VAR = & $ Result EXECUTES,
THIS $ VAR AND $ Result Are The Same, Both of @ and *
* /
// the test is the folloading
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 ();
$ OTBYREF = & $ O-> getByref ();
Println ("/ $ in:". $ IN);
Println ("/ $ OUTBYVAL:". $ outbyval);
Println ("/ $ OUTBYREF:" $ outbyf);
Println ("/ $ this-> var:". $ O-> var);
Println ("--------------- After Change -----------------");
$ in = "after change";
Println ("/ $ in:". $ IN);
Println ("/ $ OUTBYVAL:". $ outbyval);
Println ("/ $ OUTBYREF:" $ outbyf);
Println ("/ $ this-> var:". $ O-> var);
PRINTLN ();
Println ("============= setByref getByval ==============");
Println ("--------------- Before change ----------------");
$ in = "before change";
$ O-> setByref ($ IN);
$ outbyval = $ o-> getBYVAL ();
$ outbef = & $ o-> getByval ();
Println ("/ $ in:". $ IN);
Println ("/ $ OUTBYVAL:". $ outbyval);
Println ("/ $ OUTBYREF:" $ outbyf);
Println ("/ $ this-> var:". $ O-> var);
Println ("--------------- After Change -----------------");
$ in = "after change";
Println ("/ $ in:". $ IN);
Println ("/ $ OTBYVAL:" $ outbyval); Println ("/ $ OUTBYREF:". $ outbyf);
Println ("/ $ this-> var:". $ O-> var);
PRINTLN ();
Println ("============= Passbyref getByval ===============");
Println ("--------------- Before change ----------------");
$ in = "before change";
$ O-> Passbyref ($ IN);
$ outbyval = $ o-> getBYVAL ();
$ outbef = & $ o-> getByval ();
Println ("/ $ in:". $ IN);
Println ("/ $ OUTBYVAL:". $ outbyval);
Println ("/ $ OUTBYREF:" $ outbyf);
Println ("/ $ this-> var:". $ O-> var);
Println ("--------------- After Change -----------------");
$ in = "after change";
Println ("/ $ in:". $ IN);
Println ("/ $ OUTBYVAL:". $ outbyval);
Println ("/ $ OUTBYREF:" $ outbyf);
Println ("/ $ this-> var:". $ O-> var);
PRINTLN ();
/ *
The following output is that I (Night owl) arbitrarily edited, mainly for later people to see if it is convenient, more meat, apologize to LongnetPro
Output result:
============ setByref getByref =============
---------------- Before change --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
$ in: before change
$ Outbyval: Before Change
$ outbef: Before change
$ this-> var: before change
---------------- After change -----------------
$ in: After Change
$ Outbyval: Before Change
$ Outbyref: After change
$ this-> var: instator
============ setByref getByval ==============
---------------- Before change --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
$ in: before change
$ OTBYVAL: Before change $ OUTBYREF: Before Change
$ this-> var: before change
---------------- After change -----------------
$ in: After Change
$ Outbyval: Before Change
$ outbef: Before change
$ this-> var: instator
============ Passbyref getByval =============
---------------- Before change --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
$ in: before change
$ Outbyval: Before Change
$ outbef: Before change
$ this-> var: before change
---------------- After change -----------------
$ in: After Change
$ Outbyval: Before Change
$ outbef: Before change
$ this-> var: instator
* /
?>