private properties ....

From: Date: Fri, 30 Nov 2007 11:43:32 +0000
Subject: private properties ....
Groups: php.internals 
Request: Send a blank email to internals+get-33543@lists.php.net to get a copy of this message
Hi List, <?php error_reporting(E_ALL); class aaa { protected $_parent = null; private $_value = 0; public function setValue($value) { $this->_value = $value; } public function getValue) { return $this->_value; } public function setParent( $oParent ) { $this->_parent = $oParent; } public function showParentValue() { return $this->_parent->_value; } } $aa = new aaa(); $aa->setValue(500); $bb = new aaa(); $bb->setParent($aa); echo $bb->showParentValue() . PHP_EOL; ?> The code above shows me a feature thats bypass my class design. :) With this code it is possible to access the private class property _value from $aa. Output: 500 If i us a other class like : class cc { private $_value = 'foo'; } $aa = new cc(); $bb = new aaa(); $bb->setParent($aa); echo $bb->showParentValue() . PHP_EOL; i get an error that tells me cc:$_value is private and could not be accessed. Thats ok. But with this change i cant overload or change my property in any way. class cc extends aaa { private $_value = 'foo'; } $aa = new cc(); $bb = new aaa(); $bb->setParent($aa); echo $bb->showParentValue() . PHP_EOL; echo $aa->getValue() . PHP_EOL; the output is: 0 0 Thats a bit strange to me because i expected a different behavior. Conclusion: 1. Why i can access a private property from a different class instance (name) but same type ? $aa and $bb are instances of aaa but not the same. 2. This doesnt works if cc is a own class with same property name (ie. interface or something like this) 3. Is it a bug that i can't use same property name in my child class? (normaly the parent property isnt visible to the child) cc extends aaa. Thats just some questions :) -- Marco

« previous php.internals (#33543) next »