Home > Backend Development > PHP Tutorial > Detailed explanation of various usage methods of PHP reference (&)_PHP tutorial

Detailed explanation of various usage methods of PHP reference (&)_PHP tutorial

WBOY
Release: 2016-07-13 10:35:25
Original
902 people have browsed it

The reference of PHP (that is, adding the & symbol in front of a variable, function, object, etc.), the reference in PHP means: different names access the same variable content. It is different from pointers in C language. The pointer in C language stores the content of the variable and the address stored in the memory.

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

Copy the code The code is as follows:
$a="ABC";
$b =&$a;
echo $a;//Output here: ABC
echo $b;//Here Output: ABC
$b="EFG";
echo $a;//The value of $a here becomes EFG, so EFG
is output echo $b;//EFG
?> ;

2. Function transfer by reference (call by address)
I won’t go into details about call by address. The code will be given directly below

Copy code The code is as follows:

function test(&$a)
{
$a=$a+100;
}
$b=1;
echo $b;//Output 1
test($b); //What $b is passed to the function here is actually the memory address where the variable content of $b is located. The value of $b can be changed by changing the value of $a in the function
echo "
";
echo $b;//Output 101
?>

It should be noted that if test(1); is used here, an error will occur. You have to think about the reason yourself.
Note: Do not add the ampersand in front of $b in the above "test($b);", but in the function "call_user_func_array", if you want to refer to the passed parameters, you need the ampersand, as shown in the following code:

Copy 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
Look at the code first

Copy 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 will output the value of $b as 1
$a=5;
$a=test();//This statement will output the value of $b as 2
$a=&test( );//This statement will output the value of $b as 3
$a=5;
$a=test();//This statement will output the value of $b as 6
? >

Explanation below:
What you get in this way $a=test(); is not actually a reference return of the function. It is no different from an ordinary function call. The reason: This is the regulation of PHP
PHP It is stipulated that what is obtained through $a=&test(); is the reference return of the function
As for what is a reference return (the PHP manual says: reference return is used when you want to use a function to find where the reference should be bound) When the variable is above) I didn’t understand this nonsense for a long time

Using the above example to explain,
calling a function using $a=test() only assigns the value of the function to $a, and any changes to $a will not affect $b in the function.
When calling a function through $a=&test(), its function is to point the memory address of the $b variable in return $b and the memory address of the $a variable to the same place
That is, Equivalent to this effect ($a=&$b;) So changing the value of $a also changes the value of $b, so after executing
$a=&test();
$a=5;
Afterwards, the value of $b becomes 5

Static variables are used here to let everyone understand the reference return of functions. In fact, the reference return of functions is mostly used in objects

Attached is another official PHP example:

Copy code 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 = 'How';
$aa->out();
$d = 'Are';
$aa->out();
$d = 'You';
$aa->out();
?>
// the output is "HiHowAreYou"

4. Object reference

Copy code The code is as follows:
class a{
var $abc="ABC";
}
$b=new a;
$c=$b;
echo $b->abc;//Output ABC
here echo $c->abc;//Output ABC
here $b->abc="DEF";
echo $c->abc;//Output DEF
?>
here >

The above code is the running effect in PHP5

In PHP5, object assignment is a reference process. In the above column, $b=new a; $c=$b; is actually equivalent to $b=new a; $c=&$b;
The default in PHP5 is to call objects by reference, but sometimes you may want to Create a copy of an object and hope that changes to the original object will not affect the copy. For this purpose, PHP5 defines a special method called __clone.
As of PHP 5, new automatically returns a reference, so using =& here is obsolete and produces an E_STRICT level message.
In php4, object assignment is a copy process,
For example: $b=new a, where new a produces an anonymous a object instance, and $b at this time is for this anonymous object copy. In the same way, $c=$b is also a copy of the content of $b. Therefore, in php4, in order to save memory space, $b=new a will generally be changed to the reference mode, that is, $b=& new a.

Here's another official example:
In php5, you don't need to add anything else to achieve the "object reference" function:

Copy code 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 $barn");
print( $bar );
print("n");
print("Now $baz is referenced to $bar and printing $bar and $bazn");

$baz =& $bar;
print( $bar );
print("n");
print("Now Creating MasterOne and Two and passing $bar to both constructorsn");

$m1 = new MasterOne( $bar );
$m2 = new MasterTwo( $bar );
print( $m1 );
print( $m2 );
print("n");
print("Now changing value of $bar and printing $bar and $bazn");

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

$m2->setFooName( 'MasterTwo's Foo' );
print( $m1 );
print( $m2 );
print("Also printing $bar and $bazn");
print( $bar );
print( $baz );
?>

输出:

复制代码 代码如下:
Only Created $bar and printing $bar
my name is "bar" and I live in "foo".
Now $baz is referenced to $bar and printing $bar and $baz
my name is "bar" and I live in "foo".
Now Creating MasterOne and Two and passing $bar to both constructors
Master: MasterOne | foo: my name is "bar" and I live in "foo".
Master: MasterTwo | foo: my name is "bar" and I live in "foo".
Now changing value of $bar and printing $bar and $baz
my name is "baz" and I live in "foo".
my name is "baz" and I live in "foo".
Now printing again MasterOne and Two
Master: MasterOne | foo: my name is "baz" and I live in "foo".
Master: MasterTwo | foo: my name is "baz" and I live in "foo".
Now changing MasterTwo's foo name and printing again MasterOne and Two
Master: MasterOne | foo: my name is "MasterTwo's Foo" and I live in "foo".
Master: MasterTwo | foo: my name is "MasterTwo's Foo" and I live in "foo".
Also printing $bar and $baz
my name is "MasterTwo's Foo" and I live in "foo".
my name is "MasterTwo's Foo" and I live in "foo".


Analysis of the previous example:

Copy code The code is as follows:
$bar = new foo('bar') ;
$m1 = new MasterOne( $bar );
$m2 = new MasterTwo( $bar );

$bar in instance objects $m1 and $m2 is a reference to instance $bar, not a copy. This is a feature of object references in php5, that is to say
1. Inside $m1 or $m2 , any operation on $bar will affect the related value of the external object instance $bar.
2. Changes to the external object instance $bar will also affect the reference values ​​of $bar inside $m1 and $m2.

In php4, when you want to use an object instance to refer to the properties of another object as mentioned above, the equivalent code (i.e. reference call) is similar to the following:

Copy the code The code is as follows:
class foo{
var $bar;
function setBar(&$newBar){
$this->bar =& newBar ;
}
}

5. The role of references
If the program is relatively large, there are many variables referencing the same object, and you want to manually clear the object after using it, I personally recommend using the "&" method, and then use $var=null method to clear. At other times, use the default method of php5. In addition, for the transfer of large arrays in php5, it is recommended to use the "&" method, after all, it saves memory space.
6. Unreference
When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the variable contents are destroyed. For example:

Copy code The code is as follows:

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

will not unset $b, just $a.
7.global reference
When declaring a variable with global $var, a reference to the global variable is actually established. That is the same as doing:
$var =& $GLOBALS["var"];
?>
This means, for example, unset $var Global variables will not be unset.
If a reference is assigned to a variable declared as global inside a function, the reference is only visible inside the function. This can be avoided by using the $GLOBALS array.
Example Reference global variables within a function

Copy code The code is as follows:
$var1 = "Example variable";
$var2 = "";
function global_references($use_globals)
{
global $var1, $var2;
if (!$use_globals) {
$var2 =& $var1; // visible only inside the function
} else {
$GLOBALS["var2"] =& $var1; // visible also in global context
}
}
global_references(false);
echo "var2 is set to '$var2'n"; // var2 is set to ''
global_references(true);
echo "var2 is set to '$var2'n"; // var2 is set to 'Example variable'
?>

Think of global $var; as shorthand for $var =& $GLOBALS['var'];. Thus assigning another reference to $var only changes the reference to the local variable.

8.$this
In a method of an object, $this is always a reference to the object that calls it.
//Here’s another little episode
The pointing (similar to pointer) function of the address in PHP is not implemented by the user himself, but is implemented by the Zend core. The reference in PHP uses "copy-on-write" The principle is that unless a write operation occurs, variables or objects pointing to the same address will not be copied.
In layman terms
1: If there is the following code
[code] $a="ABC";
$b=&$a;
?> ;

In fact, $a and $b point to the same memory address at this time instead of $a and $b occupying different memories
2: If you add the following code to the above code

Copy code The code is as follows:
$a="EFG";
?>

Since the data in the memory pointed to by $a and $b needs to be rewritten, at this time the Zend core will automatically determine and automatically produce a data copy of $a for $b, and re-apply for a piece of memory for storage
php References (that is, adding an ampersand in front of variables, functions, objects, etc.) is an advanced topic. Newbies should pay attention. It is important to correctly understand references in PHP, which has a great impact on performance, and misunderstanding may lead to program errors!
Many people misunderstand that references in PHP are the same as pointers in C. In fact, they are not, and they are very different. Except for the pointers in C language that do not need to be explicitly declared during the array transfer process, other points need to be defined using *. However, the pointer to address (similar to a pointer) function in PHP is not implemented by the user himself, but is implemented by the Zend core. Yes, the reference in PHP adopts the principle of "copy-on-write", that is, unless a write operation occurs, variables or objects pointing to the same address will not be copied, such as the following code:

Copy code The code is as follows:
$a = array('a','c'...'n');
$b = $a;

If the program only executes here, $a and $b are the same, but they do not occupy different memory spaces like C, but point to the same memory space. This is The difference between php and c is that you don’t need to write $b=&$a to mean that $b points to the memory of $a. zend has already implemented the reference for you, and zend will be very smart to help you judge when to do this. , when should not be handled this way.
If you continue to write the following code later, add a function, pass parameters by reference, and print out the array size.
Copy code The code is as follows:
function printArray(&$arr) //pass by reference
{
print(count ($arr));

}


printArray($a);
In the above code, we pass the $a array into the printArray() function by reference. The zend engine will think that printArray() may cause the $ If a changes, a data copy of $a will be automatically produced for $b and a new piece of memory will be applied for storage. This is the "copy-on-write" concept mentioned earlier.
If we change the above code to the following:
Copy the code The code is as follows:
function printArray($arr) / /Value passing
{
print(count($arr));
}
printArray($a);

The above code directly passes the $a value to printArray(). There is no reference transfer at this time, so there is no copy-on-write.
You can test the execution efficiency of the above two lines of code. For example, add a loop outside 1000 times and see how long it takes to run. The results will let you know that incorrect use of references will cause performance to drop by more than 30%.
Self-understanding: When passing by value, it has nothing to do with the parameters in the function, which is equivalent to the role of local variables, but when passing by address (reference), it is related to the parameters within the function, which is equivalent to the role of global variables. From a performance perspective, it is enough to look at the above analysis. .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/743931.htmlTechArticlephp reference (that is, add lt;? $a="ABC" in front of variables, functions, objects, etc. ; $b = echo $a;//Output here: ABC echo $b;//Output here: ABC $b="EFG"; echo $a;//The value of $a here becomes...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template