PHP reference (&)

What is cited

         Cited in PHP means to access the same variable content by different names. This is not like C pointers: for example you can not do pointer arithmetic on them, they are not actual memory addresses. Alternatively, the reference symbol table alias. Note that in PHP, variable name and variable content are not the same, so the same content can have different names.

What do quote

PHP references allow two variables to point to the same content. I mean, when doing so:

1 <?php
2 
3 $a = &$b;
4 
5 ?>

This means that $ a and $ b point to the same variable, $ a is not pointing to $ b or vice versa.

If an undefined variable is referenced assignment, reference parameters or returns a reference, it is automatically created variables.

. 1 ? < PHP 
 2  function foo (& $ var ) {}
 . 3  
. 4 foo ( $ A ); // $ A is created and assigned to null 
. 5  
. 6  $ B = Array ();
 . 7 foo ( $ B [ 'B ' ]);
 . 8  var_dump ( array_key_exists (' B ', $ B )); // $ B [' B '] are created returns BOOL (to true) 
. 9  
10  $ C = new new StdClass;
 . 11 foo ( $ C -> D );
 12  var_dump(property_exists ( $ c , 'd')); // Create $ c d property returns BOOL (to true) 
13 is  
14 >?

If you have a reference to the variable assignment in a foreach statement, the object being referenced is also changed.

. 1 ? < PHP
 2  $ REF = 0 ;
 . 3  $ Row = & $ REF ;
 . 4  the foreach ( Array (. 1, 2,. 3) AS  $ Row ) {
 . 5      // perform operations 
. 6  }
 . 7  echo  $ REF ; // . 3 - the last element of the array iteration 
8 ?>
 

The second thing references do is to pass variables by reference. This is done by establishing a local variable within a function and the variables refer to the same content in the calling scope to achieve.

1 <?php
2 function foo(&$var)
3 {
4     $var++;
5 }
6 
7 $a=5;
8 foo($a);
9 ?>

 6 $ a become. This is because in the function foo the variable $ var refers to the same content as $ a's.

 

The third thing references do is return by reference.

 

 

Passed by reference

Note that there is no reference at function call sign - there is only a function definition. Just enough that the function is defined by parameters passed by references correctly.

The following can be passed by reference:

  • Variables, such as  foo ($ a)
  • New statements, such as  foo (new foobar ())
  • Returning a reference from a function, for example:

1 <?php
2 function &bar()
3 {
4     $a = 5;
5     return $a;
6 }
7 foo(bar());
8 ?>

Any other expression can not be passed by reference, the results are undefined.

 

Returning References

Returning References used when you want to use the function to find the references should be bound in which variable. Do not use returns a reference to increase performance, the engine is smart enough to optimize their own. Only returns a reference only when there is reasonable technical reasons! To return references, use this syntax:

 1 <?php
 2 class foo {
 3     public $value = 42;
 4 
 5     public function &getValue() {
 6         return $this->value;
 7     }
 8 }
 9 
10 $obj = new foo;
11 $myValue = &$obj->getValue(); // $myValue 是对$obj->value 的引用,该值为 42.
12 $obj->value = 2;
13 echo $myValue;                 // print $ obj-> value assignment, namely 2. 
14 >?

Dereference

When unset the reference, you just break the binding between variable name and variable content. This does not mean that variable content will be destroyed. E.g:

1 <?php
2 $a = 1;
3 $b =& $a;
4 unset($a);
5 ?>

Will not unset $ b, just $ a.

Spotting References

Many PHP syntax structure is achieved by referencing mechanism, so the above references all relevant binding also apply to these structures. Some structures, such as passing and returning by reference, have been mentioned above. Other references to the use of structure:

global  reference

When using  global $ var  you declare a variable actually creates a reference to the global variable. That and this is done the same:

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

unset $ var will not unset global variable.

 

$this

In a method object, $ this is always a call to its object.

 

 

More knowledge finishing in php.net

Guess you like

Origin www.cnblogs.com/guaiyouyisi/p/11684108.html