Bug #50892 [Com]: Protected members of non-congruent children classes are visible
Edit report at https://bugs.php.net/bug.php?id=50892&edit=1
ID: 50892
Comment by: arth dot inbox 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:
We should not to stand on other language behavior.
One more time: https://3v4l.org/TM6jk
For now it just breaks SOLID.
Previous Comments:
------------------------------------------------------------------------
[2018-05-26 14:25:45] rowan dot collins at gmail dot com
Sorry, slight mistake in my example: to be equivalent to this report, the Subclass::add(ParentClass
$other) method should be declared on one of the child classes. But it still works in both languages:
https://3v4l.org/IInDq
https://tio.run/##vVPBThsxED1vvmKOm5Ya6LXihIS4IFWA2kPVw6w9ZA1eO7JnEyrEt6fe9YbYJDkgVbX2NH5@8@bN20dc4ZdH9bTZYBPYo2SQBkOA715LgpcZxLP0jkkyKVCubwzBCk1P38a7BIhVLdObOsfMJ4bhcKuDGKtwkTO87njeNOx1XBDf4vrH8Kqe73feoa6c75DjywmbCTg9hUs0JgBm/B1x6xS4B1i3Wra0Ig@y1UZNPugwCn8j8cS9t3CD3Arveqvqca5C33w72Otsllju8fnKEyVT6ZnJqncWpzkK3FEnQ78kX6fyttXhRRWyMoZpit1KSpokZuW0AlSqTnIct@T/hZ8j0aFYfL5Id@LAtjMzRzk/NbfRrdLMl700bmH/00v4BOfiayZ7NilK8m9Q2/i8moqBkbdmd/GqvmOv7eLXb0C/CEOjqipigfH3sbQuslKfn4mzqL@qiqmbCZoXd9C7P4GpE65nsYwt2dgaxYFf6Bi4OQqOXwzHNXk6AbSgbRzSDhF6KBO@1sYASknhfYi6JuYmwnPlkRXFkMfmw/KHNWw2fwE
------------------------------------------------------------------------
[2018-05-26 14:11:34] rowan dot collins at gmail dot com
This may seem odd, but is actually perfectly reasonable if you consider polymorphism:
- Access is class-based, not instance-based: you can call private members of a different instance of
the same class.
- For the same reason, you can access protected members on an instance of a parent class.
- If you ask for an instance of class A, you may at run-time actually receive an instance of some
sub-class of A.
- So, if classes B1 and B2 both inherit from A, a method on class B1 might ask for an instance of A,
and be given an instance of B2; when it calls a method on that instance, it's the definition in
B2 which will be accessed.
Here's a concrete example: https://3v4l.org/LeClo
Note that this example also works in Java: https://tio.run/##vZMxb9swEIVn61fcKKctbWctMhUIuhgomqIdigwn8mIxpUiDPNkpDP92hxJlW4rsIUBRQhP5@O670@MzbvDTs/pzOGAR2KNkkAZDgG9eS4JdBnGtvWOSTAqUqwtDsEFT0@f2LAnirpbpTt7XTDuHZnGpg2h34a7vsD/7nBhGFVfE33H7s7mVT8eVz6p75yvkeLPT9gBmM/iCxgTAnn9FXDoF7gm2pZYlbciDLLVR3Rx0aMFPJp649haWyKXwrrYqb/sa8E1HjXWYG6cVoFJ5mq7jkvy/IGyNLg36w106Exfmt8/2WZYsfuDLvSdKVPTCZNWbBCT@ge7qjw71mnyeto@lLudoQNVz6IZ8bmRE3CL80lxGpCHxbpTIo@x/AsMNLMRtDzvriBL@ErWN1yfdZmDkYzyqeJQ/sNd29fsR0K9CU2gySUPH@HYsbQd/Il/MxTyCHzVFp@k3f9Y8/A1MlXA1i3UswsbmKC48nGvi4qo4fjHAX8nTR0AL2sa2bBPzp2FwttoYQCkpvA16VcRsR3mfPLqiaN5M8W78ZvCHwys
The example from the original report also works in Java: https://tio.run/##fY9BDoMgEEXXcgriShflAqZH6Mpl0zSg1CAKDYy2TePZKQTUpovuZv7/mf@mpzM99K10rhmotfimNX6j7D6xQTS4BiNUh69pPeKc5lXmbaOBN8DbPbEpPrQteYUWhOJlRg3mT@Cq3Vp2U4r210yds/YWcAtFGdQsuMyXKP4IJ4uy8mL9ssBHoicgd88DgyoYSdT/AitnyCyBJpVGqBMV6ovEAoUVaPRWEX8/XzA1nY104Q@Z6Pwc6SSJ/KnEuQ8
I'm of the opinion that this is not a bug.
------------------------------------------------------------------------
[2017-07-21 18:42:58] cmb@php.net
Related To: Bug #74932
------------------------------------------------------------------------
[2017-02-22 09:32:30] mail at pmmaga dot net
A PR was prepared to fix this on the current master (7.2) but it was decided the the potential BC
break could possibly be too much for a minor and it was suggested that it should wait for the next
major.
------------------------------------------------------------------------
[2016-03-27 16:16:02] nikic@php.net
Related To: Bug #69766
------------------------------------------------------------------------
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
Thread (21 messages)
- EdwardDrapkin at gmail dot com
- jani@php.net#50892 [Opn->Bgs]: Protected members of non-congruent children classes are visible
- EdwardDrapkin at gmail dot com#50892 [Bgs]: Protected members of non-congruent children classes are visible
- EdwardDrapkin at gmail dot com#50892 [Bgs]: Protected members of non-congruent children classes are visible
- colder@php.net#50892 [Bgs]: Protected members of non-congruent children classes are visible
- johannes@php.net#50892 [Bgs]: Protected members of non-congruent children classes are visible
- colder@php.net#50892 [Bgs->Opn]: Protected members of non-congruent children classes are visible
- php at b8r dot orgReq #50892 [Com]: Protected members of non-congruent children classes are visible
- php at b8r dot orgReq #50892 [Com]: Protected members of non-congruent children classes are visible
- normandiggs at gmail dot comReq #50892 [Com]: Protected members of non-congruent children classes are visible
- arth dot inbox at gmail dot comReq #50892 [Com]: Protected members of non-congruent children classes are visible
- cmb@php.netReq->Bug #50892 [Opn]: Protected members of non-congruent children classes are visible
- mail at pmmaga dot netBug #50892 [Com]: Protected members of non-congruent children classes are visible
- rowan dot collins at gmail dot comBug #50892 [Com]: Protected members of non-congruent children classes are visible
- rowan dot collins at gmail dot comBug #50892 [Com]: Protected members of non-congruent children classes are visible
- arth dot inbox at gmail dot comBug #50892 [Com]: Protected members of non-congruent children classes are visible
- giovanni at giacobbi dot netBug #50892 [Com]: Protected members of non-congruent children classes are visible
- rowan dot collins at gmail dot comBug #50892 [Com]: Protected members of non-congruent children classes are visible
- rowan dot collins at gmail dot comBug #50892 [Com]: Protected members of non-congruent children classes are visible
- teskanoo at gmail dot comBug #50892 [Com]: Protected members of non-congruent children classes are visible
- maxdumonceaux at gmail dot comBug #50892 [Com]: Protected members of non-congruent children classes are visible