Please take a lot of coaching! Quote is a very wonderful thing, there is a bit similar to the pointer in C, but it is different! In PHP, reference is just another name of the variable content. Below I have played a few more ways to explain (only php4 content, PHP5 has a big change in the use of classes and objects, and has not been studied.): 1, $ a = & $ b; create a reference $ here A. He points to the variable content points to the variable name of $ b, and it is a bit. A metaphor: Just have a cake than a drawer, B has a key, now B copies a key to A, A and B can open this drawer, eat this cake. 2, bind a return application to a variable:
Code
$ a = & new someclass (); $ a = & call_function ();
The New operator returns a copy of the instance of someClass, binds this copy to $ A by reference operator, which means $ A pointing to this instance. If you use assignment operattles =, then this instance will be copied, and $ a is to point to this new copy of this new replication. 3, quote transmission
Code
Function SomeFunction (& $ a) {$ A = 10;} $ a = 10; SomeFuntion ($ a); // now $ a is 20
Through this method, the variables outside the function range can be operated within the function range. This usage feels similar to the pointer in C. For example, I write a function that opens the file.
Code
Function OpenSomefile (& $ fd) {Global $ error; $ fd = @fopen ("somefile"); if ($ fd) {return true;} else {$ error = "can not open file";}}
Then you can write the program that you know with C:
Code
// Initialize two variables $ fd = null; $ error = null; if (OpenSomefile ($ fd)) {// do something} else {EXIT ($ error);}
Note that when you define a function, add your & symbol in front of the metropolis, you will not need it in the place where this function is called. This is not the same as the reference below! 4, reference to return
Code
Function & foo () {// do something $ a = & new someclass (); return $ a;} $ r = & foo ();
An object is created in the function foo here, and a reference to this object, the function returns this reference. We want to use this object outside the function, you need this method, $ REFA = & foo (); this sentence binds the reference to the function foo to the variable $ refa, which means $ REFA and FOO inside $ A points to the same content. Note that there must be & symbols here in the definition function and the call function. This approach is often used when using the Factory mode design program, such as PEAR :: DB library, etc. 5. When using unset ($ var) to destroy a reference, do not destroy the content pointing to the $ VAR:
Code
$ a = 10; $ b = & $ a; echo $ a; echo "/ n"; echo $ b; echo "/ n"; unset ($ b); Echo $ A;
As the code, Unset ($ b) does not make $ A variation. Or drawers and keys, A and B have the key of the same drawer. B throws away the key, but A is still, or you can open the drawer. 6, I have just encountered a question:
Code
Function foo (& $ a) {$ a = & new someclass (); $ AAA = NULL; FOO ($ AAA);
Such usage does not allow $ AAA to point to an instance of SomeClass. why? Because when the foo is called, the metinum $ A is a reference to $ AAA, = & $ AAA; and then in the function, $ A is again re-pointing to another object. So, there is no operation of $ AAA. Just like B get the key to the drawer from A, but it is not enough, he will make this key to the drawer, then he naturally can't open the drawer.