Referencing is a wonderful thing. It's a bit like a pointer in C, but it's not the same! In PHP, the reference is only
Another name of the variable content. Let me illustrate a few examples (only the content of PhP4, in PhP5 ).
I have not carefully studied the usage of classes and objects ):
1. $ A = & $ B;
Here, a reference $ A is created, which points to the content of the variable pointed to by the variable named $ B.
Ah. A metaphor: Like a drawer with a cake and B with a key, B Copies a key.
You can open the drawer for a, a, and B to eat the cake.
2. bind an operation that returns an application to a variable:
$ A = & New someclass ();
$ A = & call_function ();
The new operator returns a copy of The someclass instance and binds this copy by referencing the operator.
Set to $ A, that is, $ A points to this instance. If the value assignment operator = is used
Copy the instance and set $ A to point to the new instance.
3. Transfer references
Function somefunction (& $ ){
$ A + = 10;
}
$ A = 10;
Somefuntion ($ );
// Now $ A is 20
This method allows you to operate variables out of the function range. Usage
It is similar to the pointer in C. For example, I write a function to open a file.
Function opensomefile (& $ FD ){
Global $ error;
$ FD = @ fopen ("somefile ");
If ($ FD ){
Return true;
} Else {
$ Error = "can not open file ";
Return false;
}
}
Then you can write a program that is familiar with C:
// Initialize two variables
$ FD = NULL;
$ Error = NULL;
If (opensomefile ($ FD )){
// Do something
} Else {
Exit ($ error );
}
Note: you only need to add the & symbol before the parameters when defining the function.
Function. This is different from the reference returned below!
4. Return a reference
Function & Foo (){
// Do something
$ A = & New someclass ();
Return $;
}
$ Refa = & Foo ();
Here, the function Foo creates an object, $ A is the reference of this object, and the function returns
This reference. We need to use this method to use this object outside the function,
$ Refa = & Foo (); this statement binds the reference returned by function Foo to the variable $ Refa.
That is to say, $ A inside $ Refa and foo points to the same content.
Note that the & symbol is required for defining and calling functions.
This method is often used when designing a program in the factory mode, such as pear: DB database
And so on.
5. When unset ($ var) is used to destroy a reference, the content pointed to by $ var will not be destroyed:
$ A = 10;
$ B = & $;
Echo $;
Echo "/N ";
Echo $ B;
Echo "/N ";
Unset ($ B );
Echo $;
The code above, unset ($ B), does not change $. Or drawer and key, A and B
All have keys for the same drawer. B threw the key away, but a still has the right to open the drawer.
6. A problem I just encountered:
Function Foo (& $ ){
$ A = & New someclass ();
}
$ AAA = NULL;
Foo ($ AAA );
This method does not allow $ AAA to point to an instance of someclass. Why? Because in
When foo is called, the parameter $ A is a reference pointing to $ AAA, that is, $ A = & $ AAA;
Inside, $ A is directed to another object again. Therefore, $ AAA is not operated. Like B.
Get the drawer a key from a, but unfortunately, he made the key into a drawer
B's, then he naturally cannot open a drawer.