PHP reference (&) Examples of various usage methods

Source: Internet
Author: User
Php reference (that is, adding a symbol before a variable, function, or object) means that different names access the same variable content in PHP. The pointer in C language is different from the pointer in C language.

Php reference (that is, adding a symbol before a variable, function, or object) means that different names access the same variable content in PHP. The pointer in C language is different from the pointer in C language.

Php references (that is, adding the & symbol before variables, functions, and objects) mean that different names access the same variable content. The pointer is different from the pointer in C language. the pointer in C Language stores the variable content and the address stored in memory.

1. variable reference
PHP reference allows you to use two variables to point to the same content

The Code is as follows:

$ A = "ABC ";
$ B = & $;
Echo $ a; // output here: ABC
Echo $ B; // output here: ABC
$ B = "EFG ";
Echo $ a; // here, the value of $ a is changed to EFG, so EFG is output.
Echo $ B; // output EFG here
?>

2. function reference transfer (Address Transfer)
I will not talk much about the address transfer call. The following code is provided directly.

The Code is as follows:

Function test (& $)
{
$ A = $ a + 100;
}
$ B = 1;
Echo $ B; // output 1
Test ($ B); // here, $ B Actually transmits the memory address of the variable content of $ B to the function, you can change the value of $ B by changing the value of $ a in the function.
Echo"
";
Echo $ B; // output 101
?>

Note that test (1); will cause an error.
Note: Do not add the & symbol before $ B in the above "test ($ B);", but in the function "call_user_func_array", to reference the parameter passing, the & symbol is required, as shown in the following code:

The Code is as follows:

Function a (& $ B ){
$ B ++;
}
$ C = 0;
Call_user_func_array ('A', array (& $ c ));
Echo $ c;
// Output 1
?>


3. function reference return
First look at the code

The Code is as follows:

Function & test ()
{
Static $ B = 0; // declare a static variable
$ B = $ B + 1;
Echo $ B;
Return $ B;
}
$ A = test (); // This statement outputs the value of $ B as 1.
$ A = 5;
$ A = test (); // This statement outputs the value of $ B to 2.
$ A = & test (); // This statement outputs the value of $ B to 3.
$ A = 5;
$ A = test (); // This statement outputs a value of 6 for $ B.
?>

The following explains:
In this way, $ a = test (); is not actually returned by the function reference, which is no different from the normal function call. The reason is: this is the PHP rule.
PHP requires that $ a = & test (); is used to obtain the function reference and return.
As for what is reference return (in the PHP manual, reference return is used when you want to use a function to find the variable on which the reference should be bound .) I haven't understood this sentence for a long time.

The example above is as follows:
$ A = test () is used to call a function. It only assigns the value of the function to $ a. Any change made to $ a does not affect $ B in the function.
But how to call a function through $ a = & test, the function is to direct the memory address of the $ B Variable in return $ B to the same place as the memory address of the $ a variable.
That is, the equivalent effect ($ a = & $ B;) is generated. Therefore, changing the value of $ a also changes the value of $ B.
$ A = & test ();
$ A = 5;
Later, the value of $ B is changed to 5.

Static variables are used to help you understand the reference and return functions. In fact, function reference and return are mostly used in objects.

Another official php example is provided:

The Code is as follows:

This is the way how we use pointer to access variable inside the class.
Class talker {
Private $ data = 'Hi ';
Public function & get (){
Return $ this-> data;
}

Public function out (){
Echo $ this-> data;
}
}
$ Aa = new talker ();
$ D = & $ aa-> get ();
$ Aa-> out ();
$ D = 'who ';
$ Aa-> out ();
$ D = 'all ';
$ Aa-> out ();
$ D = 'you ';
$ Aa-> out ();
?>
// The output is "HiHowAreYou"

4. Object Reference

The Code is as follows:

Class {
Var $ abc = "ABC ";
}
$ B = new;
$ C = $ B;
Echo $ B-> abc; // output ABC here
Echo $ c-> abc; // output ABC here
$ B-> abc = "DEF ";
Echo $ c-> abc; // output DEF here
?>

The code is running in PHP5

In PHP5, object assignment is a reference process. In the above column, $ B = new a; $ c = $ B; is equivalent to $ B = new a; $ c = & $ B;
In PHP5, the object is called by reference by default, but sometimes you may want to create a copy of the object and expect that the change of the original object will not affect the copy. for this purpose, PHP5 defines a special method called _ clone.
Since PHP 5, new automatically returns a reference. Therefore, when = & is used here, messages at the E_STRICT level will be generated.
In php4, the value assignment of an object is a copy process,
For example, $ B = new a, where new a generates an anonymous a object instance, and $ B Copies the anonymous object. Similarly, $ c = $ B is a copy of $ B content. Therefore, in php4, to save memory space, $ B = new a is generally changed to the reference mode, that is, $ B = & new.

The following is an official example:
In php5, you can get the "Object Reference" function without adding anything else:

The Code is as follows:

Class foo {
Protected $ name;
Function _ construct ($ str ){
$ This-> name = $ str;
}
Function _ toString (){
Return 'My name is "'. $ this-> name.'" and I live in "'. _ CLASS _.'". '. "\ n ";
}
Function setName ($ str ){
$ This-> name = $ str;
}
}
Class MasterOne {
Protected $ foo;
Function _ construct ($ f ){
$ This-> foo = $ f;
}
Function _ toString (){
Return 'master: '. _ CLASS _.' | foo: '. $ this-> foo. "\ n ";
}
Function setFooName ($ str ){
$ This-> foo-> setName ($ str );
}
}
Class MasterTwo {
Protected $ foo;
Function _ construct ($ f ){
$ This-> foo = $ f;
}
Function _ toString (){
Return 'master: '. _ CLASS _.' | foo: '. $ this-> foo. "\ n ";
}
Function setFooName ($ str ){
$ This-> foo-> setName ($ str );
}
}
$ Bar = new foo ('bar ');
Print ("\ n ");
Print ("Only Created \ $ bar and printing \ $ bar \ n ");
Print ($ bar );
Print ("\ n ");
Print ("Now \ $ baz is referenced to \ $ bar and printing \ $ bar and \ $ baz \ n ");

$ Baz = & $ bar;
Print ($ bar );
Print ("\ n ");
Print ("Now Creating MasterOne and Two and passing \ $ bar to both constructors \ n ");

$ M1 = new MasterOne ($ bar );
$ M2 = new MasterTwo ($ bar );
Print ($ m1 );
Print ($ m2 );
Print ("\ n ");
Print ("Now changing value of \ $ bar and printing \ $ bar and \ $ baz \ n ");

$ Bar-> setName ('baz ');
Print ($ bar );
Print ($ baz );
Print ("\ n ");
Print ("Now printing again MasterOne and Two \ n ");
Print ($ m1 );
Print ($ m2 );
Print ("\ n ");
Print ("Now changing MasterTwo's foo name and printing again MasterOne and Two \ n ");

$ M2-> setFooName ('mastertwo \'s foo ');
Print ($ m1 );
Print ($ m2 );
Print ("Also printing \ $ bar and \ $ baz \ n ");
Print ($ bar );
Print ($ baz );
?>

Output:

The Code is as follows:

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.