php的引用(就是在变量或者函数、对象等前面加上&符号)
在php 中引用的意思是:不同的名字访问同一个变量内容。
与c语言中的指针是有差别的.c语言中的指针里面存储的是变量的内容,在内存中存放的地址。
1.变量的引用
php 的引用允许你用两个变量来指向同一个内容
$a = abc ;
$b =& $a ;
echo $a ; // 这里输出:abc
echo $b ; // 这里输出:abc
$b = efg ;
echo $a ; // 这里$a的值变为efg 所以输出efg
echo $b ; // 这里输出efg
?>
2.函数的引用传递(传址调用)
传址调用我就不多说了 下面直接给出代码
php
function test( & $a )
{
$a = $a + 100 ;
}
$b = 1 ;
echo $b ; // 输出1
test( $b ); // 这里$b传递给函数的其实是$b的变量内容所处的内存地址,通过在函数里改变$a的值 就可以改变$b的值了
echo
;
echo $b ; // 输出101
?>
要注意的是,在这里test(1);的话就会出错,原因自己去想。
注意:
上面的“ test($b); ” 中的$b前面不要加 & 符号,但是在函数“call_user_func_array”中,若要引用传参,就得需要 & 符号,如下代码所示:
php
function a( & $b ){
$b ++ ;
}
$c = 0 ;
call_user_func_array ( ' a ' , array ( & $c ));
echo $c ;
// 输出 1
?>
3.函数的引用返回
先看代码
php
function & test()
{
static $b = 0 ; // 申明一个静态变量
$b = $b + 1 ;
echo $b ;
return $b ;
}
$a = test(); // 这条语句会输出 $b的值 为1
$a = 5 ;
$a = test(); // 这条语句会输出 $b的值 为2
$a =& test(); // 这条语句会输出 $b的值 为3
$a = 5 ;
$a = test(); // 这条语句会输出 $b的值 为6
?>
下面解释下:
通过这种方式$a=test();得到的其实不是函数的引用返回,这跟普通的函数调用没有区别 至于原因: 这是php的规定
php规定通过$a=&test(); 方式得到的才是函数的引用返回
至于什么是引用返回呢(php手册上说:引用返回用在当想用函数找到引用应该被绑定在哪一个变量上面时。) 这句狗屁话 害我半天没看懂
用上面的例子来解释就是
$a=test()方式调用函数,只是将函数的值赋给$a而已, 而$a做任何改变 都不会影响到函数中的$b
而通过$a=&test()方式调用函数呢, 他的作用是 将return $b中的 $b变量的内存地址与$a变量的内存地址 指向了同一个地方
即产生了相当于这样的效果($a=&$b;) 所以改变$a的值 也同时改变了$b的值 所以在执行了
$a=&test();
$a=5;
以后,$b的值变为了5
这里是为了让大家理解函数的引用返回才使用静态变量的,其实函数的引用返回多用在对象中
另附一个php官方例子:
this is the way how we use pointer to access variable inside the class .
php
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.对象的引用
php
class a{
var $abc = abc ;
}
$b = new a;
$c = $b ;
echo $b -> abc; // 这里输出abc
echo $c -> abc; // 这里输出abc
$b -> abc = def ;
echo $c -> abc; // 这里输出def
?>
以上代码是在php5中的运行效果
在php5中 对象的赋值是个引用的过程。上列中$b=new a; $c=$b; 其实等效于$b=new a; $c=&$b;
php5中默认就是通过引用来调用对象, 但有时你可能想建立一个对象的副本,并希望原来的对象的改变不影响到副本 . 为了这样的目的,php5定义了一个特殊的方法,称为__clone。
自 php 5 起,new 自动返回引用,因此在此使用 =& 已经过时了并且会产生 e_strict 级别的消息。
在php4中,对象的赋值是个拷贝过程,
如:$b=new a,其中new a产生的是一个匿名的a对象实例,而此时的$b是对这个匿名对象的拷贝。同理$c=$b,也是对$b内容的一个拷贝。所以在php4中,为了节省内存空间,$b=new a 一般会改成引用的模式,即 $b=& new a。
下面再来个 官方 提供的例子:
在php5中,你不需要额外添加什么东西就可到达“对象引用”的功能:
php
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 );
?>
输出:
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.
上个例子解析:
$bar = new foo( ' bar ' );
$m1 = new masterone( $bar );
$m2 = new mastertwo( $bar );
实例对象$m1与$m2中的$bar是对实例$bar的引用,而非拷贝,这是php5中,对象引用的特点,也就是说
1.$m1或$m2内部,任何对$bar的操作都会影响外部对象实例$bar的相关值。
2.外部对象实例$bar的改变也会影响$m1和$m2内部的$bar的引用相关值。
在php4中,要实现如上述的 用一个对象实例去当着另外一个对象的属性时,其等价代码(即引用调用)类似如下:
class foo{
var $bar ;
function setbar( & $newbar ){
$this -> bar =& newbar;
}
}
5.引用的作用
如果程序比较大,引用同一个对象的变量比较多,并且希望用完该对象后手工清除它,个人建议用 & 方式,然后用$var=null的方式清除. 其它时候还是用php5的默认方式吧. 另外, php5中对于大数组的传递,建议用 & 方式, 毕竟节省内存空间使用。
6.取消引用
当你 unset 一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了。例如:
php
$a = 1 ;
$b =& $a ;
unset ( $a );
?>
不会 unset $b,只是 $a。
7.global 引用
当用 global $var 声明一个变量时实际上建立了一个到全局变量的引用。也就是说和这样做是相同的:
php
$var =& $globals [ var ];
?>
这意味着,例如,unset $var 不会 unset 全局变量。
如果在一个函数内部给一个声明为 global 的变量赋于一个引用,该引用只在函数内部可见。可以通过使用 $globals 数组避免这一点。
example 在函数内引用全局变量www.2cto.com
php
$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'
?>
把 global $var; 当成是 $var =& $globals['var']; 的简写。从而将其它引用赋给 $var 只改变了本地变量的引用。
8.$this
在一个对象的方法中,$this 永远是调用它的对象的引用。
//下面再来个小插曲
php中对于地址的指向(类似指针)功能不是由用户自己来实现的,是由zend核心实现的,php中引用采用的是“写时拷贝”的原理,就是除非发生写操作,指向同一个地址的变量或者对象是不会被拷贝的。
通俗的讲
1:如果有下面的代码
$a = abc ;
$b =& $a ;
?>
其实此时 $a与$b都是指向同一内存地址 而并不是$a与$b占用不同的内存
2:如果在上面的代码基础上再加上如下代码
php
$a = efg ;
?>
由于$a与$b所指向的内存的数据要重新写一次了,此时zend核心会自动判断 自动为$b生产一个$a的数据拷贝,重新申请一块内存进行存储
php的引用(就是在变量或者函数、对象等前面加上&符号)是个高级话题,新手多注意,正确的理解php的引用很重要,对性能有较大影响,而且理解错误可能导致程序错误!
很 多人误解php中的引用跟c当中的指针一样,事实上并非如此,而且很大差别。c语言中的指针除了在数组传递过程中不用显式申明外,其他都需要使用*进行定 义,而php中对于地址的指向(类似指针)功能不是由用户自己来实现的,是由zend核心实现的,php中引用采用的是“写时拷贝”的原理,就是除非发生 写操作,指向同一个地址的变量或者对象是不会被拷贝的,比如下面的代码:
$a = array ( ' a ' , ' c ' ... ' n ' );
$b = $a ;
如 果程序仅执行到这里,$a和$b是相同的,但是并没有像c那样,$a和$b占用不同的内存空间,而是指向了同一块内存,这就是php和c的差别,并不需要 写成$b=&$a才表示$b指向$a的内存,zend就已经帮你实现了引用,并且zend会非常智能的帮你去判断什么时候该这样处理,什么时候不 该这样处理。
如果在后面继续写如下代码,增加一个函数,通过引用的方式传递参数,并打印输出数组大小。
function printarray( & $arr ) // 引用传递
{
print ( count ( $arr ));
}
printarray( $a );
上面的代码中,我们通过引用把$a数组传入printarray()函数,zend引擎会认为printarray()可能会导致对$a的改变,此时就会自动为$b生产一个$a的数据拷贝,重新申请一块内存进行存储。这就是前面提到的“写时拷贝”概念。
如果我们把上面的代码改成下面这样:
function printarray( $arr ) // 值传递
{
print ( count ( $arr ));
}
printarray( $a );
上面的代码直接传递$a值到printarray()中,此时并不存在引用传递,所以没有出现写时拷贝。
大家可以测试一下上面两行代码的执行效率,比如外面加入一个循环1000次,看看运行的耗时,结果会让你知道不正确使用引用会导致性能下降30%以上。
自我理解:按传值的话是与函数内的参数无关,相当于局部变量的作用,而按传址(引用)的话却与函数内的参数有关,相当于全局变量的作用.而从性能方面来说,看上面分析就够..
引用做什么
php 的引用允许用两个变量来指向同一个内容。意思是,当这样做时:
这意味着 $a 和 $b 指向了同一个变量。
note:
$a 和 $b 在这里是完全相同的,这并不是 $a 指向了 $b 或者相反,而是 $a 和 $b 指向了同一个地方。
note:
如果具有引用的数组被拷贝,其值不会解除引用。对于数组传值给函数也是如此。
note:
如果对一个未定义的变量进行引用赋值、引用参数传递或引用返回,则会自动创建该变量。
example #1 对未定义的变量使用引用
d);
var_dump(property_exists($c, 'd')); // bool(true)
?>
同样的语法可以用在函数中,它返回引用,以及用在 new 运算符中(php 4.0.4 以及以后版本):
自 php 5 起, new 自动返回引用,因此在此使用 =& 已经过时了并且会产生 e_strict 级别的消息。
note:
不用 & 运算符导致对象生成了一个拷贝。如果在类中用 $this,它将作用于该类当前的实例。没有用 & 的赋值将拷贝这个实例(例如对象)并且 $this 将作用于这个拷贝上,这并不总是想要的结果。由于性能和内存消耗的问题,通常只想工作在一个实例上面。
尽管可以用 @ 运算符来抑制构造函数中的任何错误信息,例如用 @new,但用 &new 语句时这不起效果。这是 zend 引擎的一个限制并且会导致一个解析错误。
warning
如果在一个函数内部给一个声明为 global 的变量赋于一个引用,该引用只在函数内部可见。可以通过使用 $globals 数组避免这一点。
example #2 在函数内引用全局变量
把 global $var; 当成是 $var =& $globals['var']; 的简写。从而将其它引用赋给 $var 只改变了本地变量的引用。
note:
如果在 foreach 语句中给一个具有引用的变量赋值,被引用的对象也被改变。
example #3 引用与 foreach 语句
引用做的第二件事是用引用传递变量。这是通过在函数内建立一个本地变量并且该变量在呼叫范围内引用了同一个内容来实现的。例如:
将使 $a 变成 6。这是因为在 foo 函数中变量 $var 指向了和 $a 指向的同一个内容。更多详细解释见 引用传递。
引用做的第三件事是引用返回
数组引用的一个bug(后来仔细推敲,其实不是bug)
it appears that references can have side-effects. below are two examples. both are simply copying one array to another. in the second example, a reference is made to a value in the first array before the copy. in the first example the value at index 0 points to two separate memory locations. in the second example, the value at index 0 points to the same memory location.
i won't say this is a bug, because i don't know what the designed behavior of php is, but i don't think any developers would expect this behavior, so look out.
an example of where this could cause problems is if you do an array copy in a script and expect on type of behavior, but then later add a reference to a value in the array earlier in the script, and then find that the array copy behavior has unexpectedly changed.
php
// example one
$arr1 = array ( 1 );
echo \nbefore:\n ;
echo \$arr1[0] == { $arr1 [0]}\n ;
$arr2 = $arr1 ;
$arr2 [ 0 ] ++ ;
echo \nafter:\n ;
echo \$arr1[0] == { $arr1 [0]}\n ;
echo \$arr2[0] == { $arr2 [0]}\n ;
输出:
before:
$arr1[0] == 1
after:
$arr1[0] == 1
$arr2[0] == 2
// example two
$arr3=array(1);
$a=&$arr3[0];
echo\nbefore:\n;
echo\$a == $a\n;
echo\$arr3[0] == {$arr3[0]}\n;
$arr4=$arr3;
$arr4[0]++;
echo\nafter:\n;
echo\$a == $a\n;
echo\$arr3[0] == {$arr3[0]}\n;
echo\$arr4[0] == {$arr4[0]}\n;
输出:
before:
$a == 1
$arr3[0] == 1
after:
$a == 2
$arr3[0] == 2
$arr4[0] == 2
?>
分析说明:
对于“example two”,刚开始还以为是个bug,其实仔细推敲,非也,分析如下,
在赋值(拷贝)
$arr4 = $arr3 ;
之前,还有个对$arr3的第一个元素建立引用的过程,即
$a =& $arr3 [ 0 ];
所以在后来的赋值拷贝( $arr4=$arr3; ),会把这个引用一并拷贝过去,所以说
$a、$arr3[0]、$arr4[0] 三者其实是引用关系,指向同一个地方。
摘自 ppp
http://www.bkjia.com/phpjc/478314.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/478314.htmltecharticlephp的引用(就是在变量或者函数、对象等前面加上符号) 在php 中引用的意思是:不同的名字访问同一个变量内容。 与c语言中的指针是有...