Page 1 of 1
Inheritance in interfaces?
Posted: Thu Dec 07, 2006 7:21 am
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.
Posted: Thu Dec 07, 2006 9:12 am
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#
Posted: Thu Dec 07, 2006 9:30 am
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

Posted: Thu Dec 07, 2006 11:11 am
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.
Posted: Thu Dec 07, 2006 4:09 pm
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

Posted: Thu Dec 07, 2006 4:16 pm
by Luke
that pretty much sums it up then...

Posted: Thu Dec 07, 2006 4:20 pm
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.
Posted: Thu Dec 07, 2006 4:54 pm
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)
Posted: Thu Dec 07, 2006 5:09 pm
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"