Edit report at http://bugs.php.net/bug.php?id=52044&edit=1
ID: 52044 User updated by: public at proside dot fr Reported by: public at proside dot fr Summary: Access protected method violation Status: Bogus Type: Bug Package: Class/Object related Operating System: WIN XP SP3+ PHP Version: 5.3.2 New Comment: Ok, many thanks mister :-) Fast, brilliant and useful Previous Comments: ------------------------------------------------------------------------ [2010-06-11 11:52:23] degeb...@php.net Hi, Please see my answer here: http://news.php.net/php.doc.cvs/6808 $a is "standalone", but it is MyParent instance used within another MyParent instance. It's not leaking any information to other classes, so it isn't violating any contracts. ------------------------------------------------------------------------ [2010-06-11 11:12:22] public at proside dot fr I almost do agree about your comment, but in my example, $a is a standalone instance of the parent class. Where $a can be used doesn't really matter, it's still a standalone instance of class like any general instance of class. And in the general case the PHP engine logically prevent the access to the protected methods. Here, it seems that the context of execution of a standalone object impacts the contract defined by the OOP rules ! ------------------------------------------------------------------------ [2010-06-11 10:28:44] degeb...@php.net Yes, you are wrong. One MyParent object knows the private/protected interface of all other MyParent objects, so it is allowed to access it. Other classes don't, so they're not allowed. Thanks for your interest though. ------------------------------------------------------------------------ [2010-06-11 10:20:25] public at proside dot fr Description: ------------ In one case, the PHP engine allows access to the protected methods of an instanciated class As the documentation says : protected methods are only available inside the class that declares it and inside the tree of derivated classes. If you look at the code, we are in none of these case : $a is a standalone instance of MyParent so the protected methods of class MyParent should be hidden. Am i wrong ? Test script: --------------- <?php class MyParent { protected function getProtectedParent() { return 'Parent_Protected'; } } class MyChild extends MyParent { function getPublicChild() { # --> new standalone instance of MyParent $a = new MyParent(); # --> here it's possible to access to the protected method of $a ! return $a->getProtectedParent(); } } $cls = new MyChild(); $test = $cls->getPublicChild(); Expected result: ---------------- Fatal error : access level violation Actual result: -------------- $test = 'Parent_Protected' ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=52044&edit=1