Real understanding of referencing & in PHP-variable reference, function reference, object reference

Source: Internet
Author: User

Real understanding of referencing & in PHP-variable reference, function reference, object reference

PHP references (that is, the variables or functions, objects, etc. with the & symbol)//The most important is to delete the referenced variable, but the reference variable can not access, but the content is not destroyed in PHP refers to the meaning of: different names to access the same variable content.

A reference to a variable
PHP references allow you to use two variables to point to the same content

<? php$a = "ABC"$b =&$a$a;  $b; //$b = "EFG"$a;  here a $ A value becomes EFG so the output EFG echo $b;//Output EFG?> here           

Address invocation of a function call I'm not going to say much. The following code is given directly

<? phpfunction test (&$a) {     $a =$a +100$b =1$b;  Output 1 Test ($b);//Here $b passed to the function is actually the memory address of the $b variable content, by changing the value of $ A in the function can change the value of the $b echo "<br>"; echo $b;//Output 101?> 


It is important to note that here Test (1), the word will be wrong, because: PHP rules to pass the reference cannot be a constant (can see the error hint).

a reference to a function returns the code first

<?PhpFunction &Test () {Static$b = 0;//Declare a static variable$b = $b +1echo  $b  return  $b  $a =test (); // This statement will output $b value of 1  $a = 5;  $a =test (); // This statement will output a value of $b 2 $a = &test (); // This statement will output $b value of 3  $a = 5;  $a =test (); //      


This is explained below: By this way $a=test (); The resulting is not the function of the reference return, which is not the same as the normal function call as to the reason: this is PHP provisions PHP provisions through $a=&test (); The only way to get a reference to a function is to return the reference to what is a quote (the PHP manual says that reference return is used when you want to use a function to find out which variable the reference should be bound to. I haven't read this bullshit.

Using the above example to explain is $a =test () method call function, just assign the value of the function to $ A, and $ A does not affect the function of $b, and the function is called by $a=&test (), his role is to The memory address of the $b variable in the return $b points to the same place as the memory address of the $ A variable, which results in the equivalent ($a =&b;) So changing the value of $ A also changes the value of the $b so that it executes $a =&test (); $a = 5; Later, the value of the $b becomes 5

This is to let you understand that the function reference returns only use static variables, in fact, the function of the reference return is more used in the object

references to Objects

<?Phpclass a{var  $ABC = "abc" ;}  $b =new A;  $c = $b echo  $b->abc; here output ABC echo  $c abc // here output ABC $b->abc= "DEF"; echo  $c->abc; here output def?>          

The above code is the result of the run effect in PHP5 in PHP5 the copy of the object is implemented by reference . The above $b=new A; $c = $b; is actually equivalent to $b=new A; $c =& $b; The default in PHP5 is to invoke the object by reference, but sometimes you might want to make a copy of the object, and you want the original object to change without affecting the copy. For this purpose, PHP defines a special method called __clone.

the role of references
If the program is larger, referencing the same object is more variable, and you want to use the object after the manual removal of it, the personal suggestion "&" method, and then $var=null to clear. At other times, it's the default way to use PHP5. In addition, in the PHP5 for large arrays of delivery, it is recommended to use "&" mode, after all, saving memory space to use.

dereference When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the contents of the variable are destroyed. For example:

<?php $a = 1; $b =& $a; unset ($a);?>

Not unset $b, just $a. function Quotetest () {global $var;//equivalent to $var = & $GLOBALS [' var ']; unset ($var);//Delete just delete the reference and the referenced content still exists. Ibid. This does not mean that the contents of the variable were destroyed} $var =1;quotetest (); Echo $var; Results 1

----------------------------------------------------------------------------------------------

Not unset $b, just $a.

function Quotetest () {global $var;//equivalent to $var = & $GLOBALS [' var ']; $var = 5;//Because they all point to the same memory contents} $var =1;quotetest (); Echo $var; Results 5----------------------------------------------------------------------------------------------

' & ' that's the quote

The global reference actually establishes a reference to a global variable when declaring a variable with the global $var. In other words, it is the same as doing this:

<?php $var =& $GLOBALS ["var"];?>

This means, for example, that the unset $var does not unset global variables.

$this in the method of an object, $this is always a reference to the object that called it.

Here's another episode. The pointer to the address in PHP (similar to pointers) is not implemented by the user themselves, is implemented by the Zend Core, PHP refers to the use of "copy-on-write" principle, that is, unless a write operation, a pointer to the same address of the variable or object will not be copied.

Popular Talk 1: If there is the following code [PHP] $a = "ABC"; $b = $a; [/php] In fact at this time $a and $b are pointing to the same memory address and not $ A and $b occupy different memory

2: If the code above is based on the following code [PHP] $a = "EFG"; [/php] because a $ A and $b point to the memory of the data to be re-written once, at this time Zend core will automatically decide to automatically produce a $ A copy of the data for $b, re-request a piece of memory for storage

Real understanding of referencing & in PHP-variable reference, function reference, object reference

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.