• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

References and Aliases are Different Mechanisms (zkarakaya )

发布: 2007-7-01 15:06 | 作者: admin | 来源: | 查看: 12次 | 进入软件测试论坛讨论

领测软件测试网 References and Aliases are Different Mechanisms  
Author:  zkarakaya  
Date  14/03/2001  
<b>Aliasing and Referencing are completely different mechanisms in PHP.</b>
If you are Java or C++ programmer, you must be careful when using
Objects created on run-time.
<p>
Lets see an example;
<pre>
<?
class MyClass{
   var $myData;
   var $outManager;
    cfunction MyClass($p){
      $this->myData=$p;
      $this->outManager = new MyOutManager($this);
   }
    cfunction display(){
      $this->outManager->display();
   }
}
class MyOutManager{
   var $refObj;
    cfunction MyOutManager(&$obj){
      $this->refObj = &$obj;
   }
    cfunction display(){
      echo $this->refObj->myData;
   }
}
$myvar = new MyClass(10);
$myvar->myData = 20;
$myvar->display();
?>
</pre>
What value be the output of this program code. Many programmer will
say "20", but this is not correct. Output is 10. Why? Because we have
created an instance of MyClass type on the right hand side of assignment
operator, and gave an initial value of 10. In the constructor of MyClass,
we have send the memory location of that newly created instance to another
object of type MyOutManager, and hold this value in $refObj. Now the
reference count for this object is 1, which is $refObf property of
outManager instance. Lets continue the execution. Constructor has finished
its job and returned to assignment operator. PHP4 now creates a new
reference named $myvar to newly created object. Now the reference count
of that object is 2. Be careful that $myvar is not an alies. So that when
you execute the next statement, which assigns a value 20 to its property
named $myData, PHP4 creates a new instance of MyClass type, copies
the contents of old one which is also referenced by its member outManager.
And then changes the contents of myData to 20.
<p>
>From now on you will have two different instance of type MyClass.
Our intend was not this. So to correct this problem, use alias
on the object creation statement, that is use;
<pre>
$myvar = &new MyClass(10);
</pre>
This will solve the problem. So if you are C++ and Java programmer,
you must be careful in writing PHP codes.
<p>
This description does not conflict with the information given in the
<a href="http://www.zend.com/zend/art/ref-count.php"> PHP 4: Reference
Counting and Aliasing</a>
written by <a href="http://www.zend.com/comm_person.php?id=6" >Andi
Gutmans.</a>
<p>
Ziya Karakaya

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网