accessing protected method of 'delegated' object ...

php.internals

Jochem Maas

20 years ago
if I have an object with a given base class and it has an object as a property which has the same base class should I or should I not be able to call a protected method on the contained (delegated) from the first (container) object? currently I am not able to do what I thought should work, below is an example which hopefully makes it clear what i'm going on about. if this behaviour is expected I'd be very grateful if anyone could shed some light on why. kind regards, Jochem $> php -r ' abstract class A { function test() { $this->doit(); } protected function doit() { echo "A\n"; } } class B extends A { protected function doit() { parent::doit(); echo "B\n"; } } class C extends A { function __construct() { $this->delegate = new B; } protected function doit() { $this->delegate->doit(); echo "C\n"; } /* the problem can be solved here by chaing the preceeding line of code to: protected function doit() { $this->delegate->test(); echo "C\n"; } but that does not solve the realworld issue I had regarding this :-) */ } $b = new B; $c = new C; $b->test(); $c->test(); ' A B Fatal error: Call to protected method B::doit() from context 'C' in Command line code on line 5

Marcus Börger

20 years ago
Hello Jochem, Friday, December 23, 2005, 1:28:36 PM, you wrote:
> if I have an object with a given base class and it > has an object as a property which has the same base class > should I or should I not be able to call a protected method > on the contained (delegated) from the first (container) object?
of course not it is a different object. If we had subclasses this would be different.
> currently I am not able to do what I thought should work, below > is an example which hopefully makes it clear what i'm going on about.
Just to make this clear: your code as given below must fail because it violates visibility/inheritance rules.
> if this behaviour is expected I'd be very grateful if anyone > could shed some light on why.
The problem is that we neither have 'friend' which would help you here nor do we have a second visibility rule set (however no other language supports that) and also programming languages do support grant models like databases do.
> kind regards, > Jochem
$>> php -r '
> abstract class A { > function test() { $this->doit(); } > protected function doit() { echo "A\n"; } > } > class B extends A { > protected function doit() { parent::doit(); echo "B\n"; } > } > class C extends A { > function __construct() { $this->delegate = new B; } > protected function doit() { $this->delegate->doit(); echo "C\n"; } > /* > the problem can be solved here by chaing the preceeding line of code to: > protected function doit() { $this->delegate->test(); echo "C\n"; } > but that does not solve the realworld issue I had regarding this :-) > */ > }
> $b = new B; $c = new C; > $b->test(); > $c->test(); > ' > A > B
> Fatal error: Call to protected method B::doit() from context 'C' in Command line code on line 5
Best regards, Marcus