Inheritance in interfaces?

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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Inheritance in interfaces?

Post by Chris Corbyn »

Code: Select all

interface Foo {
	public function doFoo();
}

interface Bar extends Foo {
	public function doBar();
}
Just a quick question really. I need to do what I just did above but although it works is this a flaw in PHP5 which they'll "fix" in future, resulting in my code breaking or is it an acceptable procedure in OOP?

I don't want to have to have my class implement multiple interfaces in this particular instance because the interfaces are mostly about clarity and identity.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://de2.php.net/language.oop5.interfaces wrote: Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.

All methods declared in an interface must be public, this is the nature of an interface.
That includes inheritance. And it's a common concept.
e.g. http://java.sun.com/j2se/1.4.2/docs/api ... e]nterface DataOutput

All Known Subinterfaces:
ImageOutputStream, ObjectOutput [/quote]same with C#
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Perfect :) Thanks.

It's basically for event listeners... sort of. They're called event listeners like in Java although the events are not triggered via a UI of course :)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

could you give a simple example of how this could be useful?
cause I kind of see it pointless to this point...and would consider it poor design more than anything else.
thanks.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

jmut wrote:could you give a simple example of how this could be useful?
cause I kind of see it pointless to this point...and would consider it poor design more than anything else.
thanks.
Poor design how? Is it poor design to extend a class? It's the same principle.

The reason I need it is that I'm doing something Java-esque and creating Event objects and EventListener objects.

Now, in Java (Swing/AWT) you have for example a Action event and in order to respond to the Action event you need to have an object implementing the AcionListener interface and you bind that to the swing component it acts on.

In my library I have similar triggers like Send events and SendListener objects to respond to them. But while I need a SendListener to implement the appropriate interface it also needs to implement some things from the BeforeSend event listener. I could easily force the developer to implement both interfaces but I'd rather it was transparent to the developer how the underbelly is structured since it's clearer to Joe Bloggs when he write an event listener to just implement the interface for that listener rather than remembering the specific case of where several interfaces are needed.

I often have empty interfaces of abstract classes *purely* for identity among a group of objects too. Now that I know is probably bad design but I do it anyway :)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

that pretty much sums it up then... :lol:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

From doing a little bit of digging, "extends" is the proper form of extending an interface further. I can't really see the core behind them being changed, although I don't have a snapshot of 6 around to see if the newer core performs the same.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah that just reminded me about changes in the OO model I'm waiting for in 6. Namely namespacing. I just checked the dev list and am now left with mixed feelings as to how useful it's going to be. I'd hoped they would set it up so you could "import" a namespace and therefore use short names for brevity/clarity.

However, it seems that is going to be too much work and cause a slowdown in the engine since it wasn't planned up-front so they have agreed that it will be in 6 but only in a fashion which forces you to specify the namespace on each call to a class. Therefore it's effectively the same as prefxing and PEAR naming. They've not quite committed themselves to that plan but the thinking is that it will effectively just be allowing class names to have special characters in them, with a bit of fluffy stuff on the edges.

http://marc.theaimsgroup.com/?l=php-dev ... 301439&w=2 (Rasumus Chimes in briefly)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

that's a major bummer... I thought they were going to do the import thing too... :( Although I can understand why they don't want to give up speed for "syntax sugar"
Post Reply