Php reference (variable and function name prefix & symbol) usage

Source: Internet
Author: User
Tags function definition

 

Referencing in PHP means accessing the same variable content with different names. This is not like the pointers of C. They are symbol table aliases. Note that in PHP, the variable name and variable content are different, so the same content can have different names. PHP reference is implemented by adding the & symbol before the variable name or function name. The following describes the usage of references:

Measure the test taker's knowledge about the official method.


Reference
PHP references allow two variables to point to the same content. This means that when you do this:

<? Php
$ A = & $ B;
?>
This means that $ a and $ B point to the same variable.
Note:

$ A and $ B are exactly the same here. This doesn't mean $ a points to $ B or vice versa, but $ a and $ B point to the same place.

 

Note:

If an array with a reference is copied, its value is not removed from the reference. This is also true for passing values from arrays to functions.


Note:

If an undefined variable is referenced, assigned, referenced, or returned, the variable is automatically created.

Example #1 use reference for undefined variables

<? Php
Function foo (& $ var ){}

Foo ($ a); // $ a is "created" and assigned to null

$ B = array ();
Foo ($ B ['B']);
Var_dump (array_key_exists ('B', $ B); // bool (true)

$ C = new StdClass;
Foo ($ c-> d );
Var_dump (property_exists ($ c, 'D'); // bool (true)
?>


The same syntax can be used in functions, which return references, and in the new operator (PHP 4.0.4 and later versions ):

<? Php
$ Bar = & new fooclass ();
$ Foo = & find_var ($ bar );
?>
Since PHP 5, new automatically returns a reference. Therefore, when = & is used here, messages at the E_STRICT level will be generated.

Note:

No & operator is used to generate a copy of the object. If $ this is used in the class, it will act on the current instance of the class. The instance (such as an object) will be copied without a value & and $ this will apply to this copy, which is not always the desired result. Due to performance and memory consumption problems, you usually only want to work on one instance.

Although the @ operator can be used to suppress any error information in the constructor, such as @ new, it does not work when the & new statement is used. This is a limitation of the Zend Engine and may cause a parsing error.

 

The first is a simple reference of a variable. You can use two variables to point to the same content. For example:

<? Php
$ A = 5;
$ B = & $;
Echo $ B;
$ A ++;
Echo $ B;
?>
Run this code to let $ B reference the content of $ a and change the content of $ a. The content of $ B also changes. The same syntax can be used in functions, which return references, and in the new operator:

<? Php
$ Bar = & new fooclass ();
$ Foo = & find_var ($ bar );
?>
The second thing to do with referencing is to use referencing to pass variables. This is achieved by creating a local variable in the function and referencing the same content in the call range. The common point is that a function parameter is a reference to a local variable. The following is an example

<? Php
Function foo (& $ val1, $ val2 ){
$ Val1 + = 1;
$ Val2 + = 1;
}
$ A = 5;
$ B = 10;
Foo ($ a, $ B );
Echo $;
Echo $ B;
?>
Run this code to pass two parameters to the function. One is the content that references $ a and the other is the value of $ B. After executing this function, we find that the content of $ a has changed, the content of $ B remains unchanged.
The third usage of PHP reference is reference return, which is difficult to understand. Reference return is used when you want to use a function to find the variable on which the reference should be bound. When a reference is returned, this syntax is used: the simple point is the return of the referenced function. However, unlike parameter passing, the & symbol must be used in both the function definition and function reference. The following is an example:

<? Php
Function & find_var ($ param)
{
/*... Code ...*/
Return $ found_var;
}
$ Foo = & find_var ($ bar );
$ Foo-> x = 2;
?>
In this example, assigning a value to $ foo is the return reference of the find_var function. Therefore, when assigning a value to $ foo-> x, it is to assign a value to the return reference of find_var, rather than a simple value.
The last usage of PHP reference is reference positioning. There are two main applications: one is global reference. When a variable is declared using global $ var, a reference to the global variable is actually created. That is, it is the same as $ var = & $ GLOBALS ["var. The other is $ this. In an object method, $ this is always a reference to the object that calls it.

Difference from pointer
References are similar to pointers, but they are not pointers. See the following code:

<? Php
$ A = 0;
$ B = &;
Echo $ a; // 0
Unset ($ B );
Echo $ a; // 0
Because $ B is only the alias of $ a, even if $ B is released, $ a has no effect, but the pointer is not like this. See the following code:

# Include <stdio. h>
Int main (int argc, char const * argv []) {
Int a = 0;
Int * B = &;

Printf ("% in", a); // 0
Free (B );
Printf ("% in", a); // *** error for object 0x7fff6350da08: pointer being freed was not allocated
}
B is a pointer to a, so after B's memory is released, an error will occur when accessing a. It clearly shows the difference between PHP reference and C pointer.

Objects and references
When using objects in PHP, you are always told that "objects are passed by reference". In fact, this is a misunderstanding. PHP's object variable stores a identifier of this object. When passing an object, it actually passes this identifier instead of a reference. See the following code:

<? Php
$ A = new;
$ B = $;
$ B-> testA = 2;

/*
* Relationships between $ a and $ B:
* + ----------- ++ ----------------- +
* $ A --> | object id | ---> | object (Class A) |
* + ----------- ++ ----------------- +
* ^
* + ----------- + |
* $ B --> | object id | --------- +
* + ----------- +
 *
 *
*/

$ C = new B;
$ A = $ c;
$ A-> testB = "Changed Class B ";

/*
* Relationships between $ a, $ B, and $ c:
* + ----------- ++ ----------------- +
* $ B --> | object id | ---> | object (Class A) |
* + ----------- ++ ----------------- +
 *                              
* + ------------ +
* $ A --> | object id2 | ------------- +
* + ------------ + |
* V
* + ------------ ++ ----------------- +
* $ C --> | object id2 | ---> | object (Class B) |
* + ------------ ++ ----------------- +
*/

Echo "object a:"; var_dump ($ a); // ["testB"] => string (15) "Changed Class B"
Echo "object B:"; var_dump ($ B); // ["testA"] => int (2)
Echo "object c:"; var_dump ($ c); // ["testB"] => string (15) "Changed Class B"
If the object is passed by reference, the output content of $ a, $ B, and $ c should be the same. In fact, this is not the case. See the following example:

<? Php
$ Aa = new;
$ Bb = & $ aa; // reference
$ Bb-> testA = 2;

/*
* Relationship between $ aa and $ bb:
 *
* + ----------- ++ ----------------- +
* $ Bb --> | object id | ---> | object (Class A) |
* + ----------- ++ ----------------- +
* ^
* |
* $ Aa --------- +
 *
 *
*/

$ Cc = new B;
$ Aa = $ cc;
$ Aa-> testB = "Changed Class B ";

/*
* Relationships between $ aa, $ bb, and $ cc:
 *
* + ----------- ++ ----------------- +
* | 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 (15) "Changed Class B"
Echo "object bb:"; var_dump ($ bb); // ["testB"] => string (15) "Changed Class B"
Echo "object cc:"; var_dump ($ cc); // ["testB"] => string (15) "Changed Class B"
At this time, the content of $ aa, $ bb, and $ cc is exactly the same, so we can see that the object is not transmitted by reference, and we need to get out of this misunderstanding as soon as possible.

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.