|
Posted by Summercool on 09/27/07 15:11
seems like in PHP 5, $obj1 = $obj2 is not the same as $obj1 =& $obj2
although the manual says...
http://www.php.net/manual/en/language.operators.assignment.php
> Since PHP 4, assignment by reference has been supported,
> using the $var = &$othervar
> As of PHP 5, objects are assigned by reference
> unless explicitly told otherwise with the new clone
> keyword.
However, $obj1 = $obj2
and $obj1 = &$obj2 are two different things...
the first one is "assignment by reference"
the second one is "assignment by synonym"
----------------- assign1.php ---------------------
<?php
class Foo {
var $name;
function Foo($i) {
$this->name = "I'm $i!\n";
}
}
$a = new Foo("ha");
$b = $a;
$b = new Foo("hee");
echo "This is php ", phpversion(), "\n\n";
print_r($a);
print_r($b);
?>
----------------- output
This is php 5.2.4
Foo Object
(
[name] => I'm ha!
)
Foo Object
(
[name] => I'm hee!
)
----------------- assign2.php ---------------------
<?php
class Foo {
var $name;
function Foo($i) {
$this->name = "I'm $i!\n";
}
}
$a = new Foo("ha");
$b = &$a;
$b = new Foo("hee");
echo "This is php ", phpversion(), "\n\n";
print_r($a);
print_r($b);
?>
----------------- output
This is php 5.2.4
Foo Object
(
[name] => I'm hee!
)
Foo Object
(
[name] => I'm hee!
)
[Back to original message]
|