您好,欢迎访问一九零五行业门户网

php引用是什么

php引用的意思
在php 中引用的意思是:不同的名字访问同一个变量内容。
与c语言中的指针是有差别的.c语言中的指针里面存储的是变量的内容,在内存中存放的地址。
1、变量的引用
php 的引用允许你用两个变量来指向同一个内容
<?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 "<br>"; echo $b;//输出101?>
注意:上面的“ test($b); ” 中的$b前面不要加 & 符号,但是在函数“call_user_func_array”中,若要引用传参,就得需要 & 符号,如下代码所示:
<?phpfunction a(&$b){ $b++;}$c=0;call_user_func_array('a',array(&$c));echo $c;//输出 1?>
3、函数的引用返回
<?phpfunction &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手册上说:引用返回用在当想用函数找到引用应该被绑定在哪一个变量上面时。)
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中 对象的赋值是个引用的过程。上列中$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中,你不需要额外添加什么东西就可到达“对象引用”的功能:
<?phpclass 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 );?>
推荐视频教程:php视频教程
以上就是php引用是什么的详细内容。
其它类似信息

推荐信息