[PHP] References in PHP4 ____php

Source: Internet
Author: User

A reference is a wonderful thing, a bit like a pointer in C, but it's not the same. In PHP, references are just
A different name for the variable content. Let me make a few examples to illustrate (just PHP4 content, PHP5
There has been a great change in the use of classes and objects and have not been studied carefully:

1, $a =& $b;
This creates a reference $a, pointing to the variable content that the variable named $b is pointing to, wordy.
Ah. Analogy: Like a drawer with a cake, B has a key, now B copy a key
For A,a and B, you can open the drawer and eat the cake.

2. Bind a return applied operation to a variable:
$a =& new SomeClass ();
$a =& call_function ();
The new operator returns the copy of the instance of SomeClass, which is bound by the reference operator
Set to $a, that is to say, $a point to this instance. If you are using the assignment operator =, then the
Copy the instance and let $a point to the newly replicated instance.

3. Reference delivery
Function Somefunction (& $a) {
$a + 10;
}
$a = 10;
Somefuntion ($a);
Now $a is 20

In this way, you can manipulate variables outside the scope of the function within the scope of the function. This sense of usage
Sleep 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 very familiar program with C:
Initialization of two variables
$FD = null;
$error = null;
if (Opensomefile ($FD)) {
Do something
} else {
Exit ($error);
}

Note that you only need to add the & symbol in front of the parameter when defining the function,
A function where you don't need it. This is not the same as the following reference.

4. Reference return
function &foo () {
Do something
$a =& new SomeClass ();
return $a;
}

$refa =& foo ();

Here the function Foo creates an object, $a is the reference to the object, and the function returns the
This reference. We want to use this object outside of the function, we need to use this method,
$refa =& foo (); This phrase binds the reference returned by function foo to the variable $refa, and also
That means $refa and Foo's internal $a point to the same content.

Note that there are & symbols in the place where you define functions and call functions.

This approach is often used when designing programs using the factory pattern, such as pear::D B Library
Wait a minute.

5, use unset ($var) to destroy a reference, will not destroy $var points:
$a = 10;
$b =& $a;

echo $a;
echo "/n";
Echo $b;
echo "/n";
Unset ($b);
echo $a;

As on the code, unset ($b), and will not let $a also change. Drawers and keys, A and B.
All have the keys to the same drawer. b Throw away the key, but a still, you can open the drawer.

6, I have just encountered a problem:
function foo (& $a) {
$a =& new SomeClass ();
}
$AAA = null;
Foo ($AAA);

Such a usage does not allow $AAA to point to an instance of SomeClass. Why, then? Because in
When Foo is invoked, the formal parameter $a is a reference to the $AAA, that is, the $a =& $aaa;
Number inside, the $a is again pointing to another object. So, there is no operating $aaa. It's like B.
Got the key to open the drawer from a, but unfortunately he made the key into a drawer.
        B, then naturally he can not open the drawer a slightly.

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.