#34931 [Bgs]: Problem maintaining membeprivacy while returning private member array variable.
| From: | feralcab at gmail dot com | Date: | Fri, 21 Oct 2005 14:12:00 +0000 |
| Subject: | #34931 [Bgs]: Problem maintaining membeprivacy while returning private member array variable. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-86872@lists.php.net to get a copy of this message | ||
ID: 34931
User updated by: feralcab at gmail dot com
Reported By: feralcab at gmail dot com
Status: Bogus
Bug Type: Arrays related
Operating System: FreeBSD 5.4
PHP Version: 5.0.5
New Comment:
Hi again, yes I read the link that you pointed out, and I understand
now that it is a design choice. I also read the
http://bugs.php.net/how-to-report.php site
before reporting what I
thought was a bug, when in fact it is expected behavior. I just came to
think seeing how most (if not all) C++ compilers along with Java
run-time envs behave, that an object's private data members, whether
accessed by value or reference should only be accessible within the
object's scope. Thanks for your time.
Previous Comments:
------------------------------------------------------------------------
[2005-10-21 12:16:08] tony2001@php.net
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?
------------------------------------------------------------------------
[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