设为首页 加入收藏

TOP

PHP引用(&)使用详解(二)
2017-10-09 13:24:47 】 浏览:4460
Tags:PHP 引用 使用 详解
: ' . __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 声明一个变量时实际上建立了一个到全局变量的引用。也就是说和这样做是相同

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇symfony分页实现方法 下一篇php中常用的字符串比较函数strcmp..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目