#34931 [Opn->Bgs]: Problem maintaining membeprivacy while returning private member array variable.
ID: 34931
Updated by: tony2001@php.net
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:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Did you read my answer?
Previous Comments:
------------------------------------------------------------------------
[2005-10-21 12:07:29] feralcab at gmail dot com
no comment
------------------------------------------------------------------------
[2005-10-20 16:50:17] feralcab at gmail dot com
I see, but shouldn't PHP be smart
enough to know that when it returns an object's reference
which belongs to another object as a private data member,
the data referenced should not be tampered with from outside the
owner's scope? Thank you for your time.
------------------------------------------------------------------------
[2005-10-20 15:29:50] tony2001@php.net
Yes, objects are always references in PHP5.
http://www.php.net/manual/en/migration5.oop.php
------------------------------------------------------------------------
[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
Thread (6 messages)