PHP reference passing and referencing & some usage introduction _php tutorial

Source: Internet
Author: User
Tags what php
The reference in PHP is to use & to do, let me introduce you in PHP reference some usage and reference issues and events examples, welcome friends into the reference.

What is a reference?

Referencing in PHP means accessing the same variable content with a different name. This is not like the C pointer, instead, the reference is the symbol table alias. Note that in PHP, variable names and variable contents are not the same, so the same content can have different names. The closest analogy is the Unix filename and the file itself-the variable name is the directory entry, and the variable content is the file itself. References can be seen as hardlink in Unix file systems.

Reference to do what
PHP references allow two variables to point to the same content.
When $a =& $b; When $a and $b point to the same variable.
Tip: $a and $b are exactly the same here, not $a point to $b or vice versa, but $a and $b point to the same place.

You can pass a variable to a function by reference, so that the function can modify the value of its argument. The syntax is as follows:

The code is as follows Copy Code

function foo (& $var)
{
$var + +;
}

$a = 5;
Foo ($a);
echo $a;

The output is: 6


PHP References &

For PHP references (that is, variables or functions, objects, etc., preceded by the & symbol), let's look at the following procedure.

The code is as follows Copy Code


$a = 100; Declaring variable A
$b = & $a; Declaring variable B, referring to argument a
echo "$a
";
echo "$b
";
$a + +; Variable a self-increment 1
echo "$a
";
echo "$b
";//view variable B, also increased by 1, indicating that the same storage unit is used
?>

Program Run Result:

100
100
101
101

Many people misunderstand that the reference in PHP is the same as the pointer in C, which is not, in fact, very different. In addition to the pointer in the C language is not explicitly stated in the process of the array, the others need to be defined using *, and PHP's point-of-address (similar pointer) function is not implemented by the user itself, is implemented by the Zend Core, PHP reference in the use of "copy-on-write" principle, Unless a write operation occurs, a variable or object that points to the same address is not copied.

PHP defaults to pass value:

The code is as follows Copy Code


$a = 20;
$b = $a;
$a = $a + 10;
echo $a. ' and '. $b;
?>

Program Run Result:

20 and

If you want to change address delivery, you need to add &

The code is as follows Copy Code


$a = 20;
$b = & $a;
$a = $a + 10;
echo $a. ' and '. $b;
?>

Program Run Result:

That means,& the address of $ A to $b, so the two variables now share a memory area, meaning their values are the same.

The same syntax can be used in a function, it returns a reference, and is used in the new operator:

The code is as follows Copy Code

View Sourceprint?
1

2 $bar =& new Fooclass ();

3 $foo =& Find_var ($bar);

4?>

The second thing a reference does is pass a variable by reference. This is done by creating a local variable within the function and referencing the same content within the call range. The popular point is that a function parameter is a reference to a local variable. Here is another example:

The code is as follows Copy Code

function foo (& $val 1, $val 2) {
$val 1 + = 1;
$val 2 + = 1;
}
$a = 5;
$b = 10;
Foo ($a, $b);
echo $a;
Echo $b;
?>

Running this code is to pass two parameters to the function, one is to refer to $ A content, one is the value of $b, after executing this function, we find that the contents of $ A change, and the contents of $b are unchanged.


PHP References and pitfalls


References in PHP can be interpreted as aliases of variables. Since PHP's variable name is stored in the symbol table, the variable content is stored in the heap, the reference is to use the symbol table of different symbols (symbol) name to access the same storage content, and UNIX file system Hardlink is the same concept, such as:

The code is as follows Copy Code

$a = 1;
$b = & $a; $a point to the same content as $b
$b = 2;
Echo $b; 2
echo $a; 2
Passing references
The reference pass is simple, which is a "&" symbol, such as:

function foo (& $a) {
$a = 2;
}

$b = 1;
Foo ($b);
Echo $b; 2

return reference
In most cases, it is not necessary to return a reference to improve performance, the Zend engine optimizes itself, but if you have to return a reference, you can return the reference in the following way:

The code is as follows Copy Code

class Foo {
public $value = 42;

Public Function &getvalue () {//Requires a ' & '
return $this->value;
}
}

$obj = new Foo;
$myValue = & $obj->getvalue (); A "&" is also required, $myValue is a reference to the $value in class Foo
$obj->value = 2; Modifying the $value property of an object
Echo $myValue; Output 2, $myValue the same as the $value value in class Foo

The difference from the pointer
A reference is like a pointer, but it is not a pointer, see the following code:

The code is as follows Copy Code
$a = 0;
$b = &a;
echo $a; 0
Unset ($b);
echo $a; 0

Since $b is just a $ A alias, even if $b is released, $a has no effect, but the pointer is not like this, see the following code:

The code is as follows Copy Code

#include
int main (int argc, char const *argv[]) {
int a = 0;
int* B = &a;

printf ("%in", a); 0
Free (b);
printf ("%in", a); Error for object 0x7fff6350da08:pointer being freed is not allocated
}

Since B is a pointer to a, so after releasing the memory of B, then access a will be an error, it is obvious that the PHP reference and the C pointer difference.

Objects and references
When using objects in PHP, people are always told that "objects are passed by reference", in fact, this is a misunderstanding. The object variable in PHP stores an identifier for this object, and in passing the object, it actually passes the identifier, not the reference, to see the following code:

The code is as follows Copy Code

$a = new A;
$b = $a;
$b->testa = 2;

/*
* $ A at this time, $b relationship:
* +-----------+ +-----------------+
* $a | Object ID | ---> | Object (Class A) |
* +-----------+ +-----------------+
* ^
* +-----------+ |
* $b | Object ID | ---------+
* +-----------+
*
*
*/

$c = new B;
$a = $c;
$a->testb = "Changed Class B";

/*
* At this time $ A, $b, $c relationship:
* +-----------+ +-----------------+
* $b | Object ID | ---> | Object (Class A) |
* +-----------+ +-----------------+
*
* +------------+
* $a | Object Id2 | -------------+
* +------------+ |
* V
* +------------+ +-----------------+
* $c | Object Id2 | ---> | Object (Class B) |
* +------------+ +-----------------+
*/

Echo "Object A:"; Var_dump ($a); ["Testb"]=> string ("Changed Class B")
Echo "Object B:"; Var_dump ($b); ["TestA"] = + int (2)
echo "Object C:"; Var_dump ($c); ["Testb"]=> string ("Changed Class B")

If the object is passed by reference, then $ A, $b, $c output should be the same, in fact this is not the case. Look at the following precedents for passing objects by reference:

The code is as follows Copy Code

$AA = new A;
$BB = & $aa; Reference
$BB->testa = 2;

/*
* At this time $aa, $BB relationship:
*
* +-----------+ +-----------------+
* $bb | Object ID | ---> | Object (Class A) |
* +-----------+ +-----------------+
* ^
* |
* $AA---------+
*
*
*/

$CC = new B;
$AA = $CC;
$aa->testb = "Changed Class B";

/*
* At this time $aa, $BB, $cc relationship:
*
* +-----------+ +-----------------+
* | Object ID | ---> | Object (Class A) |
* +-----------+ +-----------------+
*
* $BB---->-----+
* |
* $AA---->-----+
* |
* V
* +------------+
* | Object Id2 | --------------+
* +------------+ |
* V
* +------------+ +-----------------+
* $cc | Object Id2 | ---> | Object (Class B) |
* +------------+ +-----------------+
*/

echo "Object aa:"; Var_dump ($AA); ["Testb"]=>string ("Changed Class B")
echo "Object bb:"; Var_dump ($BB); ["Testb"]=>string ("Changed Class B")
Echo "Object cc:"; Var_dump ($CC); ["Testb"]=>string ("Changed Class B")

At this time $aa, $BB, $cc three content exactly the same, so you can see that the object is not passed by reference, to get out of this misunderstanding as soon as possible.

http://www.bkjia.com/PHPjc/628637.html www.bkjia.com true http://www.bkjia.com/PHPjc/628637.html techarticle The reference in PHP is to use mdash; the variable name is the directory entry, and the variable content is the file itself. References can be seen as hardlink in Unix file systems. Reference to do what PHP references ...

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