• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

References and Aliases are Different_php

php 搞代码 7年前 (2018-06-19) 176次浏览 已收录 0个评论

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.

{post_permalink}References and Aliases are Different_php

<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&quot; >Andi
Gutmans.</a>
<p>
Ziya Karakaya

欢迎大家阅读《References and Aliases are Different_php》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:References and Aliases are Different_php
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址