Re: Class Access Modifiers
| From: | Chad Fulton | Date: | Wed, 09 Mar 2011 18:23:16 +0000 |
| Subject: | Re: Class Access Modifiers | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-51647@lists.php.net to get a copy of this message | ||
Hello,
On Wed, Mar 9, 2011 at 10:02 AM, Jarrod Nettles <JNettles@inccrra.org> wrote:
> Interesting question. My gut tells me not (as does three years of C# experience). I’m sure
> that everyone will have a different opinion on this but to me it seems taboo that a child class
> override the visibility of the parent class. For example, PHP currently does not allow you to
> override a method with a lower or higher visibility than the parent – I can’t go from protected
> to public or vice versa. Visibility must be maintained throughout the class hierarchy.
>
Actually, class properties and methods can have a higher visibility
than their parents, just not a lower one.
E.g.:
<?php
error_reporting(E_ALL | E_STRICT);
class foo {
protected function bar() {
}
}
class baz extends foo {
public function bar() {
}
}
class foobar extends baz {
protected function bar () {
}
}
?>
You get the following error:
Fatal error: Access level to foobar::bar() must be public (as in class
baz) in ...
-------
That said, I wouldn't think that visibility modifiers on classes need
to follow the same pattern. In the case of properties and methods, I
think the rationale is that child classes should be compatible from an
interface standpoint with their parents. That same logic may not
transfer to class visibility modifiers.
I am certainly no expert, but I'm curious what the use case is for
class visibility modifiers?
On Wed, Mar 9, 2011 at 7:11 AM, Hannes Landeholm <landeholm@gmail.com> wrote:
> Currently I'm forced to use huge internal classes for my framework because
dividing them into smaller classes would expose internal behavior to the
"outside"... the application layer.
>
This doesn't necessarily make sense to me. Isn't it the
end-developer's problem if they start instantiating classes in weird
ways?
It doesn't seem the same as having a private method, since in that
case the end-developer presumably already has an object instance, and
you want to guide them to the correct interface for interacting with
it, whereas in an application, one would think that the end-developer
wouldn't simply be instantiating classes by themselves all over the
place.
Chad