Bug #50892 [Com]: Protected members of non-congruent children classes are visible

From: Date: Fri, 13 Jan 2023 17:23:55 +0000
Subject: Bug #50892 [Com]: Protected members of non-congruent children classes are visible
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-243441@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=50892&edit=1 ID: 50892 Comment by: teskanoo at gmail dot com Reported by: EdwardDrapkin at gmail dot com Summary: Protected members of non-congruent children classes are visible Status: Open Type: Bug Package: Class/Object related Operating System: Irrelevant PHP Version: 5.3.1 Block user comment: N Private report: N New Comment: This really is a travesty that this scoping issue has been present for at almost 13 years and has gone unaddressed It's really not something that should be swept under the carpet Previous Comments: ------------------------------------------------------------------------ [2021-02-01 13:21:54] cmb@php.net Related To: Bug #77039 ------------------------------------------------------------------------ [2018-05-26 16:49:14] rowan dot collins at gmail dot com @giovanni: Why would the expected behaviour there be to print 10? Re-declaring a property doesn't create a second property with the same name, it changes it polymorphically just like re-declaring a method does. Either the method can see the value 10, or it cannot see any value. class C { protected $x = 10; public function m() { print "x=" . $this->x . "\n"; } } class D extends C { protected $x = 20; } $d = new D(); $d->m(); // "x=20" ------------------------------------------------------------------------ [2018-05-26 16:42:19] rowan dot collins at gmail dot com > https://3v4l.org/TM6jk This appears to be exactly the opposite complaint from the original: in this case, the protected property *can't* be accessed in a different child. So we certainly have an inconsistency here - between methods and properties, and based on whether the member is inherited directly or over-ridden / re-declared. The case of a re-declared property is currently the only one that errors, so the simplest change would be to remove that error. Class C knows that the access is legal when given an A, so assumes it's valid for all sub-classes of A; sub-class B2 is currently able to break this assumption for a property, but not for a method. ------------------------------------------------------------------------ [2018-05-26 15:21:19] giovanni at giacobbi dot net I agree with arth dot inbox at gmail dot com, the original bug report is wrong but this actually should be filed as a new bug imho: Reproduce code: --------------- class C { protected $x = 10; } class D extends C { public function m(C $c) { print "x=" . $c->x . "\n"; } } class E extends C { protected $x = 20; } $d = new D(); $e = new E(); $d->m($d); $d->m($e); Expected result: ---------------- x=10 x=10 /* note 10, NOT 20! */ Actual result: -------------- x=10 PHP Fatal error: Uncaught Error: Cannot access protected property E::$x in Standard input code:9 Stack trace: #0 Standard input code(21): D->m(Object(E)) #1 {main} thrown in Standard input code on line 9 ------------------------------------------------------------------------ [2018-05-26 14:31:24] arth dot inbox at gmail dot com We should not to stand on other language behavior. One more time: https://3v4l.org/TM6jk For now it just breaks SOLID. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=50892 -- Edit this bug report at https://bugs.php.net/bug.php?id=50892&edit=1

« previous php.bugs (#243441) next »