ID: 50822 Updated by: [email protected] Reported By: aron at zajlik dot hu Status: Bogus Bug Type: Scripting Engine problem Operating System: Mac OS X PHP Version: 5.3.1 New Comment:
The reason why you make methods and properties private in an object is to encapsulate things and to hide implementation specific details from the outside world, so to speak. However, it is expected that an instance of Test knows how Test works internally and therefore you are able to access private properties of other instances of Test. Test knows how Test works and Test knows there is a property called $variable in Test. It's pointless hiding the implementation of Test from Test. You'll find that similar behavior is possible in other object oriented languages (Java for instance). Previous Comments: ------------------------------------------------------------------------ [2010-01-22 15:16:49] aron at zajlik dot hu Although my example (by chance) is similar to a singleton pattern, my goal was actually very different. But regardless, there is no reason why the object $my_test should be able to directly access a private variable of $tobj. Even if it were a singleton class this would not be necessary. To say my point in a different way: - In a typical singleton pattern, the instantiated object is a private property of the singleton class, so there is no problem accessing that from the class. In fact, you are able to access public properties and methods of the instantiated object from within the singleton class. - BUT there is no reason why you should be able to access a private property of that instance! Only the instance itself should have access to that... Hope I made myself clear... peace, aron ------------------------------------------------------------------------ [2010-01-22 14:38:05] [email protected] Ever heard of "singleton" ? Also: http://php.net/manual/en/language.oop5.patterns.php So yes, this is expected and correct behaviour. ------------------------------------------------------------------------ [2010-01-22 12:45:36] aron at zajlik dot hu Description: ------------ I found a situation which to me seems like unexpected behavior. I private variable (as far as I know) should only be accessible within the object itself, and not by the "outside world". Yet this becomes a bit confusing once the object is created within an instance of itself. Is this a bug or is this expected behavior? Reproduce code: --------------- <? class Test{ private $variable = "asdf"; static function create_a_test(){ $tobj = new Test(); echo $tobj->variable; // Works! (which it shouldn't?!?) } } $my_test = new Test(); //echo $my_test->variable; // Returns Fatal error: Cannot access private property (which it should!) $my_test->create_a_test(); ?> Expected result: ---------------- I would expect the above code NOT to return the private variable on line 7. Actual result: -------------- The private variable can be accessed without a problem. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=50822&edit=1
