Bug #62814 [Nab]: It is possible to stiffen child class members visibility
| From: | rasmus@php.net | Date: | Fri, 24 Aug 2012 00:35:15 +0000 |
| Subject: | Bug #62814 [Nab]: It is possible to stiffen child class members visibility | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-172851@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62814&edit=1
ID: 62814
Updated by: rasmus@php.net
Reported by: resha dot ru at gmail dot com
Summary: It is possible to stiffen child class members
visibility
Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: Linux
PHP Version: 5.3.15
Block user comment: N
Private report: N
New Comment:
Johannes, I think you are actually agreeing with him here. As per Liskov you
can't tighten visibility, you can only loosen it on inheritance. However, in this
case you aren't tightening it because the original was private, so setting it
back to private is allowed and doesn't violate Liskov.
Previous Comments:
------------------------------------------------------------------------
[2012-08-23 21:28:49] johannes@php.net
If you'd stiffen visibility you'd be violating the is-a contract given. If an object is
using a classed derived from A and I can do "instanceof A" I expect to be able to call all
methods from A and having them do something like A's method does. When stiffening that
won't be the case anymore.
------------------------------------------------------------------------
[2012-08-23 19:49:18] kev dot simpson at gmail dot com
I reported this same problem a few years ago here: https://bugs.php.net/bug.php?id=48376
I was told this was not a bug (although I haven't a clue as to why its not deemed as such). I
believe C# allows you the ability to do this as it would resolve up the scope, but as of 5.4.0 I
still cannot get why it allows forced reduced scopes during declaration, but provides an uncatchable
fatal error on call.
I'm still most concerned by the allowed reduction from an interface. There is no way to
guarantee that an interface will implement the method in question regardless of if it is known to be
that type which truly is a shame.
------------------------------------------------------------------------
[2012-08-14 09:50:47] resha dot ru at gmail dot com
Sorry, it should be:
class F extends D
{
private function test() { } // stiffen visibility from public to private
(unexpected)
}
But nevertheless.
------------------------------------------------------------------------
[2012-08-14 09:48:17] resha dot ru at gmail dot com
Description:
------------
It is possible to stiffen visibility (from public to protected, from public to
private and from protected to private) if any of parent classes has private
modifier.
Test script:
---------------
class A
{
private function test() { }
}
class B extends A
{
protected function test() { } // loosen visibility from private to protected (expected)
}
class C extends B
{
private function test() { } // stiffen visibility from protected to private (unexpected)
}
class D extends B
{
public function test() { } // loosen visibility from protected to public (expected)
}
class E extends D
{
protected function test() { } // stiffen visibility from public to protected (unexpected)
}
class F extends B
{
private function test() { } // stiffen visibility from public to private (unexpected)
}
Expected result:
----------------
PHP Fatal error: Access level to C::test() must be protected (as in class B) or
weaker
Actual result:
--------------
Everything is ok.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62814&edit=1