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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to