免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2008 | 回复: 6
打印 上一主题 下一主题

php 面向对象? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-20 13:13 |只看该作者 |倒序浏览
class SimpleClass{
   public $var = 'a default value';
   public function displayVar(){
      echo $this->var;
  }
}

$instance = new SimpleClass();

$obj1   =  $instance; //-----1
$obj2   =&$instance; //-----2

不知1,2 有什么区别?
(1不就是传地址,$instance 与$obj1 指向同一个实例吗? 2加了一个 & ,搞不清楚了,请大家解答下.)

论坛徽章:
0
2 [报告]
发表于 2009-06-21 13:59 |只看该作者
LZ很细心,它们没有任何区别,有区别的是对象克隆功能

论坛徽章:
0
3 [报告]
发表于 2009-06-21 17:48 |只看该作者
在PHP4是有区别的!在PHP5以上第二种是会产生警告的!因为PHP5对于对象的赋值就是引用的~!而PHP4就不一样!

论坛徽章:
0
4 [报告]
发表于 2009-06-22 00:12 |只看该作者
In contrast, functions that accept array and object parameters have a performance advantage when references are used. This is because arrays and objects do not use reference counting, so multiple copies of an array or object are created if "pass by value" is used. So the following code:

function ObjRef(&$o)
{
   $a =$o->name;
}

is faster than:

$function ObjRef($o)
{
  $a = $o->name;
}

Note: In PHP 5, all objects are passed by reference automatically, without the need of an explicit & in the parameter list. PHP 5 object performance should be significantly faster

参考:
http://phplens.com/lens/php-book/optimizing-debugging-php.php

论坛徽章:
0
5 [报告]
发表于 2009-06-24 09:14 |只看该作者
原帖由 peoong 于 2009-6-20 13:13 发表
class SimpleClass{
   public $var = 'a default value';
   public function displayVar(){
      echo $this->var;
  }
}

$instance = new SimpleClass();

$obj1   =  $instance; //-----1
$obj ...

1是复制
2是引用内存地址

论坛徽章:
0
6 [报告]
发表于 2009-06-24 13:14 |只看该作者
都用到public了,肯定是php5了,都是引用,类复制要用clone

论坛徽章:
0
7 [报告]
发表于 2009-06-27 18:16 |只看该作者
参看PHP手册“引用”部分
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP