首页 > php基础 > References and Aliases are Different Mechanisms (z

References and Aliases are Different Mechanisms (z

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> 

<?php
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 


教程地址:http://www.phprm.com/base/591cf036607aeb204b0126c0edaef6e4.html

欢迎转载!但请带上文章地址^^

标签:none

发表留言