# accessing protected method of 'delegated' object ...

- Thread: https://externals.io/message/21330
- Original archive: https://news-web.php.net/php.internals/21330
- Started by Jochem Maas on 2005-12-23T12:28:36+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #21330 — Jochem Maas — 2005-12-23T12:28:36+00:00

URL: https://externals.io/message/21330#21330

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

---

## #21332 — Marcus Boerger — 2005-12-23T17:55:29+00:00

URL: https://externals.io/message/21330#21332 · In reply to: #21330

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
