Page 1 of 1

External access to base class methods

Posted: Thu Aug 04, 2005 10:14 pm
by benforbes
If I have a class 'b' that extends a class 'a', and both classes define a function 'foo', how can I invoke a's definition of foo on an instance of b? Ie:

Code: Select all

$b=new b();
//Need to run a::foo() on $b

class a {
function foo() {
}
}
class b extends a {
function foo() {
}
}

Posted: Thu Aug 04, 2005 10:26 pm
by feyd

Posted: Thu Aug 04, 2005 10:30 pm
by benforbes
That won't work if I'm calling from outside an instance of b. I need something equivalent to the following C++ code:

Code: Select all

b.a::foo();

Posted: Thu Aug 04, 2005 10:32 pm
by feyd
I don't remember that working in C++... at any rate, that's VERY bad object form if you want that. You'll need a fascade function in B to proxy into A for an outsider..

Posted: Thu Aug 04, 2005 10:39 pm
by benforbes
It works in C++, but I agree that it's not good coding. I don't actually want to use it, I was just learning PHP classes and wanted to make sure I understood fully. Thanks.

Posted: Thu Aug 04, 2005 10:46 pm
by feyd
from what I remember, A would have to be a member of B, not a parent for it to work... although it's been a few years since I last worked in C++... :? (at least, your code specifically)

I know casting B to A would work though..