Bug #50892 [Com]: Protected members of non-congruent children classes are visible
| From: | rowan dot collins at gmail dot com | Date: | Sat, 26 May 2018 14:11:38 +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-215379@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: rowan dot collins 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 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.
Previous Comments:
------------------------------------------------------------------------
[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
------------------------------------------------------------------------
[2015-09-08 00:38:08] cmb@php.net
| There is no design ground to accept that, it is a bug. The
| question that remains is: is it worth to fix BC-wise?
IMHO the question is rather: when is the BC break acceptable? 7.1
or 8.0?
------------------------------------------------------------------------
[2015-03-10 08:53:17] arth dot inbox at gmail dot com
normandiggs, your sample is irrelevant.
It's ok to access private and protected properties from same type object.
It's bad to access protected properties from different type object.
Why I hate this behavior:
http://3v4l.org/tiOC5 - we receive protected value
http://3v4l.org/uT9PC - we receive fatal error
We should not to rely to implementation of B class (see fiddles)
Another note - this behavior was broken in 5.2 version.
------------------------------------------------------------------------
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