ID: 34931 Updated by: [EMAIL PROTECTED] Reported By: feralcab at gmail dot com -Status: Open +Status: Bogus Bug Type: Arrays related Operating System: FreeBSD 5.4 PHP Version: 5.0.5 New Comment:
Yes, objects are always references in PHP5. http://www.php.net/manual/en/migration5.oop.php Previous Comments: ------------------------------------------------------------------------ [2005-10-20 15:21:47] feralcab at gmail dot com Description: ------------ When an object contains an array of other objects as a private member variable, if the array is returned through a member method, instead of a copy a reference to the private array is returned. Through this reference the member variable can be modified outside of the object's scope. Thus, violating the private member variable state. Reproduce code: --------------- class Atom { private $x = 0; public function __construct($x) {$this->x = $x;} public function setX($x) {$this->x = $x;} } class Element { private $atoms = array(); public function __construct($NMAX) { for ($i=0; $i<$NMAX; ++$i) $this->atoms[] = new Atom($i); } public function setAtoms($atoms) {$this->atoms = $atoms;} public function getAtoms() {return $this->atoms;} } $element = new Element(3); $v = $element->getAtoms(); print_r($v); $v[0]->setX(79); print_r($v); $w = $element->getAtoms(); print_r($w); Expected result: ---------------- Array ( [0] => Atom Object ( [x:private] => 0 ) [1] => Atom Object ( [x:private] => 1 ) [2] => Atom Object ( [x:private] => 2 ) ) Array ( [0] => Atom Object ( [x:private] => 79 ) [1] => Atom Object ( [x:private] => 1 ) [2] => Atom Object ( [x:private] => 2 ) ) Array ( [0] => Atom Object ( [x:private] => 0 ) [1] => Atom Object ( [x:private] => 1 ) [2] => Atom Object ( [x:private] => 2 ) ) Actual result: -------------- Array ( [0] => Atom Object ( [x:private] => 0 ) [1] => Atom Object ( [x:private] => 1 ) [2] => Atom Object ( [x:private] => 2 ) ) Array ( [0] => Atom Object ( [x:private] => 79 ) [1] => Atom Object ( [x:private] => 1 ) [2] => Atom Object ( [x:private] => 2 ) ) Array ( [0] => Atom Object ( [x:private] => 79 ) [1] => Atom Object ( [x:private] => 1 ) [2] => Atom Object ( [x:private] => 2 ) ) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=34931&edit=1
