#34931 [NEW]: Problem maintaining membeprivacy while returning private member array variable.

From: Date: Thu, 20 Oct 2005 13:21:48 +0000
Subject: #34931 [NEW]: Problem maintaining membeprivacy while returning private member array variable.
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-86807@lists.php.net to get a copy of this message
From: feralcab at gmail dot com Operating system: FreeBSD 5.4 PHP version: 5.0.5 PHP Bug Type: Arrays related Bug description: Problem maintaining membeprivacy while returning private member array variable. 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 bug report at http://bugs.php.net/?id=34931&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=34931&r=trysnapshot4 Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=34931&r=trysnapshot50 Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=34931&r=trysnapshot51 Fixed in CVS: http://bugs.php.net/fix.php?id=34931&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=34931&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=34931&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=34931&r=needscript Try newer version: http://bugs.php.net/fix.php?id=34931&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=34931&r=support Expected behavior: http://bugs.php.net/fix.php?id=34931&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=34931&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=34931&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=34931&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=34931&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=34931&r=dst IIS Stability: http://bugs.php.net/fix.php?id=34931&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=34931&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=34931&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=34931&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=34931&r=mysqlcfg

« previous php.bugs (#86807) next »