php引用是什么,php引用是什么
php引用的意思
在PHP 中引用的意思是:不同的名字訪(fǎng)問(wèn)同一個(gè)變量?jī)?nèi)容。
與C語(yǔ)言中的指針是有差別的.C語(yǔ)言中的指針里面存儲(chǔ)的是變量的內(nèi)容,在內(nèi)存中存放的地址。
1、變量的引用
PHP 的引用允許你用兩個(gè)變量來(lái)指向同一個(gè)內(nèi)容<?php
$a="ABC";
$b =&$a;
echo $a;//這里輸出:ABC
echo $b;//這里輸出:ABC
$b="EFG";
echo $a;//這里$a的值變?yōu)镋FG 所以輸出EFG
echo $b;//這里輸出EFG
?>
2、函數(shù)的引用傳遞(傳址調(diào)用)
傳址調(diào)用我就不多說(shuō)了 下面直接給出代碼<?php
function test(&$a){
$a=$a+100;
}
$b=1;
echo $b;//輸出1
test($b); //這里$b傳遞給函數(shù)的其實(shí)是$b的變量?jī)?nèi)容所處的內(nèi)存地址,通過(guò)在函數(shù)里改變$a的值 就可以改變$b的值了
echo "
";
echo $b;//輸出101
?>
注意:上面的“ test($b); ” 中的$b前面不要加 & 符號(hào),但是在函數(shù)“call_user_func_array”中,若要引用傳參,就得需要 & 符號(hào),如下代碼所示:<?php
function a(&$b){
$b++;
}
$c=0;
call_user_func_array('a',array(&$c));
echo $c;
//輸出 1
?>
3、函數(shù)的引用返回<?php
function &test(){
static $b=0;//申明一個(gè)靜態(tài)變量
$b=$b+1;
echo $b;
return $b;
}
$a=test();//這條語(yǔ)句會(huì)輸出 $b的值 為1
$a=5;
$a=test();//這條語(yǔ)句會(huì)輸出 $b的值 為2
$a=&test();//這條語(yǔ)句會(huì)輸出 $b的值 為3
$a=5;
$a=test();//這條語(yǔ)句會(huì)輸出 $b的值 為6
?>
下面解釋下:
通過(guò)這種方式$a=test();得到的其實(shí)不是函數(shù)的引用返回,這跟普通的函數(shù)調(diào)用沒(méi)有區(qū)別 至于原因: 這是PHP的規(guī)定
PHP規(guī)定通過(guò)$a=&test(); 方式得到的才是函數(shù)的引用返回
至于什么是引用返回呢(PHP手冊(cè)上說(shuō):引用返回用在當(dāng)想用函數(shù)找到引用應(yīng)該被綁定在哪一個(gè)變量上面時(shí)。)
4、對(duì)象的引用<?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中 對(duì)象的賦值是個(gè)引用的過(guò)程。上列中$b=new a; $c=$b; 其實(shí)等效于$b=new a; $c=&$b;PHP5中默認(rèn)就是通過(guò)引用來(lái)調(diào)用對(duì)象, 但有時(shí)你可能想建立一個(gè)對(duì)象的副本,并希望原來(lái)的對(duì)象的改變不影響到副本 . 為了這樣的目的,PHP5定義了一個(gè)特殊的方法,稱(chēng)為_(kāi)_clone。
自 PHP 5 起,new 自動(dòng)返回引用,因此在此使用 =& 已經(jīng)過(guò)時(shí)了并且會(huì)產(chǎn)生 E_STRICT 級(jí)別的消息。
在php4中,對(duì)象的賦值是個(gè)拷貝過(guò)程,如:$b=new a,其中new a產(chǎn)生的是一個(gè)匿名的a對(duì)象實(shí)例,而此時(shí)的$b是對(duì)這個(gè)匿名對(duì)象的拷貝。同理$c=$b,也是對(duì)$b內(nèi)容的一個(gè)拷貝。所以在php4中,為了節(jié)省內(nèi)存空間,$b=new a 一般會(huì)改成引用的模式,即 $b=& new a。
下面再來(lái)個(gè) 官方 提供的例子:
在php5中,你不需要額外添加什么東西就可到達(dá)“對(duì)象引用”的功能:<?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 );
?>
推薦視頻教程:PHP視頻教程
總結(jié)
以上是生活随笔為你收集整理的php引用是什么,php引用是什么的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php check name,check
- 下一篇: 动态规划算法php,php算法学习之动态