现在的位置: 首页 > 综合 > 正文

php 引用并不是指针

2013年07月01日 ⁄ 综合 ⁄ 共 5761字 ⁄ 字号 评论关闭

引用是什么

在 PHP 中引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针,替代的是,引用是符号表别名。注意在 PHP 中,变量名和变量内容是不一样的,因此同样的内容可以有不同的名字。最接近的比喻是 Unix 的文件名和文件本身——变量名是目录条目,而变量内容则是文件本身。引用可以被看作是 Unix 文件系统中的 hardlink。

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 "<br>";
    
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 一个引用,只是

抱歉!评论已关闭.