|
Posted by Don on 01/06/05 21:19
Hi,
Reading the PHP 5 documentation at: HYPERLINK
"http://www.php.net/manual/en/language.oop5.basic.php"http://www.php.net/man
ual/en/language.oop5.basic.php, I am confused.
In the example given, what is the difference between:
$assigned = $instance;
$reference =& $instance;
I would expect all of the var_dump to display NULL
The doc says "When assigning an already created instance of an object to a
new variable, the new variable will access the same instance as the object
that was assigned." so the above assignments seem the same to me and setting
$instance to NULL should also set $assigned to NULL.
If this is not the case and not using the '&' specifies a 'copy'
(contradicting the documentation) then what's the purpose of object cloning?
I tried the code below and find that it gives the exact same output
regardless if I am using the '&' or not so it seems to assign be reference
either way.
<?php
class SimpleClass
{
// member declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
$instance = new SimpleClass();
$assigned = $instance;
// $assigned = &$instance; // No difference if this line is used instead
$instance->var = 'Value has been changed';
var_dump($instance);
echo '<br /><br />';
var_dump($assigned);
?>
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.8 - Release Date: 1/3/2005
Navigation:
[Reply to this message]
|