You are here: in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2 « PHP Programming Language « IT news, forums, messages
in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2

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!

)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация