External access to base class methods

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
benforbes
Forum Newbie
Posts: 6
Joined: Tue Aug 02, 2005 6:56 pm

External access to base class methods

Post 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() {
}
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

benforbes
Forum Newbie
Posts: 6
Joined: Tue Aug 02, 2005 6:56 pm

Post 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();
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
benforbes
Forum Newbie
Posts: 6
Joined: Tue Aug 02, 2005 6:56 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
Post Reply