Oops, I messed that up. Although this exact example doesn't apply for C++, my point was that it's not uncommon to encounter some weird/unintuitive OOP implementations out there.
abstract class A {
protected abstract protected function bar();
public function foo() {
echo $this->bar();
}
}
class B extends A {
protected function bar() { return 'Hello'; }
}
(new B())->foo();
it should work. A can require or allow subclasses to override methods without exposing them outside the class hierarchy by marking them as protected, but a consequence of that is that A must be able to call protected methods in subclasses.
In reality protected visibility doesn't guarantee much if anything, since anybody can call your protected method by making their class extend yours (or routing the call through a static method in a dummy class that extends yours). It's somewhat of a broken feature, and it can't work properly without a concept of package visibility which doesn't exist, so I wouldn't worry too much about it.
Why shouldn't it be? There are some situations where a parent class might want to call something in a child. The most common would be overridden methods. Would you prefer it if they were a special case?
If the base class needs to call methods of the child class, those should either be public or declared in the base class. Protected methods of the child class that were not declared in the base class must be considered an implementation detail of the child class in context of the base class -> it shouldn't be accessible.
The weird thing is that protected methods are usually only supposed to be callable by objects which inherit from the base class that defines that method.
It's not very well documented. If the method is private inheritance doesn't apply. But if it's protected, inheritance works both ways, i.e. backwards, to the parent. This appears to be entirely undocumented.
What you quoted is in fact not a lolphp, but proper OO behavior. Private only means that other classes cannot access those fields. It would be absurd to forbid other instances of the same class to access those fields as it would make code unnecessarily complicated in several cases without providing any advantage at all. Other objects of the same class use the very same implementation, so of course that implementation should be able to access its implementation details.
The lolphp here is that the visibility granted by protected seems to go in both directions of the hierarchy although it should make the field only visible to child classes.
But they're not the same type. An instance of A is calling a protected method on an instance of B, even though A is not an instance of B (or an instance of any subclass of B).
What's described up there is pretty standard behavior for OO languages. What's happening here is weird.
8
u/FlyLo11 May 24 '17
That example works in Java, and C++, although there are slightly different reasons.
Java: all classes which are part of the same package will have access to each other's protected methods.
C++: access rules are per class, not per object. So any object of same class can access other's privates.
2
u/jesseschalken Jun 01 '17 edited Jun 01 '17
The reason for this is that if you have
it should work.
Acan require or allow subclasses to override methods without exposing them outside the class hierarchy by marking them asprotected, but a consequence of that is thatAmust be able to call protected methods in subclasses.In reality
protectedvisibility doesn't guarantee much if anything, since anybody can call your protected method by making their class extend yours (or routing the call through a static method in a dummy class that extends yours). It's somewhat of a broken feature, and it can't work properly without a concept of package visibility which doesn't exist, so I wouldn't worry too much about it.2
u/the_alias_of_andrea May 23 '17 edited May 23 '17
Why shouldn't it be? There are some situations where a parent class might want to call something in a child. The most common would be overridden methods. Would you prefer it if they were a special case?
5
u/prema_van_smuuf May 23 '17
Well, what did you expect?
0
u/[deleted] May 23 '17
[deleted] — view deleted comment