SitePoint Sponsor

User Tag List

Page 1 of 4 1234 LastLast
Results 1 to 25 of 91
  1. #1
    Resident Code Monkey Chris Corbyn's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    713
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Inheritance in interfaces?

    PHP Code:
    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.

  2. #2
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Really, they're going to fix it? Where did you read that?

    I don't know if it is normal behavior or not, but it works in java:

    Code:
    /**
     * 
     */
    package com.azavia.interface_inheritance;
    
    /**
     * @author Brandon Olivares
     * 
     */
    public interface Foo {
        /**
         * Does foo
         */
        void doFoo();
    }
    Code:
    /**
     * 
     */
    package com.azavia.interface_inheritance;
    
    /**
     * @author Brandon Olivares
     * 
     */
    public interface Bar extends Foo {
        /**
         * Does bar
         */
        void doBar();
    }
    Code:
    /**
     * 
     */
    package com.azavia.interface_inheritance;
    
    /**
     * @author Brandon Olivares
     *
     */
    public class InterfaceInheritance implements Bar {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    	InterfaceInheritance ih = new InterfaceInheritance();
    	ih.doFoo();
    	ih.doBar();
        }
        
        public void doFoo() {
    	System.out.println("Foo");
        }
        
        public void doBar() {
    	System.out.println("Bar");
        }
    
    }
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  3. #3
    Resident Code Monkey Chris Corbyn's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    713
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I didn't say they were going to fix it. I was wondering if it was expected behaviour and if it wasn't were they likely to change it in future.

    I never thought to test it in Java

    Cheers

  4. #4
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops, I misread your original post.

    Well either way, I'd say that if it works in Java, and works in PHP, it's probably intended behavior.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  5. #5
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think that's bad OOP as long as you follow the usual rule for inheritance ie each interface in the hierarchy should essentially be the same "thing". For example you could have a Policeman interface and an ArmedPoliceman interface which adds methods shoot() and honestIThoughtHeHadAGunMilord(). Either could be implemented by concrete policemen if I can say that without being arrested.

  6. #6
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by d11wtq View Post
    PHP Code:
    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.
    At the risk of bring up the whole "interfaces are pointless in a dynamic language" debate all over again, do you really need something like this? What purpose does it serve?

  7. #7
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath View Post
    Do you really need this? I'd wager you don't.
    Why not? It seems perfectly fine to me, and I've come across situations before where I've needed it.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  8. #8
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devbanana View Post
    Why not? It seems perfectly fine to me, and I've come across situations before where I've needed it.
    Why not isn't an answer to "why". It seems totally unnecessary from where I'm sitting, but then I use Ruby and get by quite happily without any kind of formal interface language construct by following the principle of duck typing

  9. #9
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Luke Redpath View Post
    Why not isn't an answer to "why". It seems totally unnecessary from where I'm sitting, but then I use Ruby and get by quite happily without any kind of formal interface language construct by following the principle of duck typing
    I thought rubyists liked syntactic sugar. :P
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  10. #10
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by The New Guy View Post
    I thought rubyists liked syntactic sugar. :P
    I think Rubyists like syntactic sugar, but not empty calories.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  11. #11
    Resident Code Monkey Chris Corbyn's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    713
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's for identity among other things. I can't really be bothered to explain myself again after explaining it elsewhere In brief, I have a very pluggable system but in order for it to work and "read" clearly, having interfaces is a much bigger help than having oodles of "duck typing" in my code.

  12. #12
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by The New Guy View Post
    I thought rubyists liked syntactic sugar. :P
    Interfaces aren't syntatic sugar though. If anything, they are syntatic vinegar

  13. #13
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by d11wtq View Post
    It's for identity among other things.
    But isn't an objects identity implied by its inherent interface? In other words, what do you gain with a formal interface over an implied one? Interfaces and type hints will give you some kind of runtime safety-check but it the way I see it its a false sense of security because there is no guarantee that the code is *correct* just because it implements a formal interface.

  14. #14
    SitePoint Guru Galo's Avatar
    Join Date
    May 2005
    Location
    Holland!
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath View Post
    At the risk of bring up the whole "interfaces are pointless in a dynamic language" debate all over again, do you really need something like this? What purpose does it serve?
    yeah indeed it's not that you gain anything with doing this but a little more structure and complexity, why not just use one interface for ya methode instead of 2, what's the benefit for you when extending an interface, or is this another "i don't now what it's purpose is but it looks cool" type of posts....

    YAGNI!!!
    Business as usual is off the menu folks, ...

  15. #15
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath View Post
    Interfaces aren't syntatic sugar though. If anything, they are syntatic vinegar
    Semantic sugar, perhaps??
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  16. #16
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From my POV, interfaces are useful to ensure method signatures when you are writing libraries -- especially if they are to be used by many 3rd parties. They are also useful for documentation and well, blah blah blah. That topic has been covered ad nauseum

  17. #17
    Resident Code Monkey Chris Corbyn's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    713
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jayboots View Post
    From my POV, interfaces are useful to ensure method signatures when you are writing libraries -- especially if they are to be used by many 3rd parties.
    This is precisely why I drop out of threads where this whole interfaces are pointless fire begins. Most people don't seem to consider that some of us aren't writing applications where we all know how the components fit together. I am, as you say, writing a fairly complex and highly composite library. Using interfaces simple allows me to check in one line what it would take me to check in ten lines without. This whole debate is just pointless unless were looking at the narrow scope of code which only ever gets used/modified by one person.

    Never used Swing in Java for example. Well, actually, anything in Java. Interfaces everywhere. If you want to deal with events in a GUI the way AWT actually invokes your event listeners is by checking the interface they implement:

    Code:
    public class MyActionEventHandler implements ActionListener
    {
    //

  18. #18
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Discussing best practices is never a waste of time, particularly in php. I think the objection might have been more about type hints. I don't see any harm in implementing an interface in the class definition but hints scattered throughout the code would get my attention. In general, cutting out everything which can be cut out brings the intent of the code into sharper focus.

  19. #19
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > what do you gain with a formal interface over an implied one?

    Greater flexibility. The real benifit of the use of Interfaces is when you have a class hierarchy structure that implements more than the one Interface alone.

    That's when things start to come into it, is what I've found. As to the arguement about not needing Interfaces in PHP, et al...

    Please...

  20. #20
    Resident Code Monkey Chris Corbyn's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    713
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston View Post
    > what do you gain with a formal interface over an implied one?

    Greater flexibility. The real benifit of the use of Interfaces is when you have a class hierarchy structure that implements more than the one Interface alone.
    Again, as I'm doing here:

    PHP Code:
    class MyPlugin implements ConnectListenerSendListener
    {
        public function 
    sendPerformed(SendEvent $e) {
        }

        public function 
    connectPerformed(ConnectEvent $e) {
        }


  21. #21
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need formal interfaces to "implement" more than one interface. Where is the "greater flexibility"?

  22. #22
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff View Post
    I don't see any harm in implementing an interface in the class definition but hints scattered throughout the code would get my attention. In general, cutting out everything which can be cut out brings the intent of the code into sharper focus.

    Hi. How exactly does a type hint do anything BUT bring the intent of the code into sharper focus?

  23. #23
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm taking the view that hints are simply documentation and documentation is unecessary clutter which should be removed in favour of more expressive naming. There are exceptions eg dependency injection tool like Phemto.

    I take your point that a hint provides some clear information about type but if I don't actually need to know that it's just extra data I have to sift through (and maintain) before I get to what I'm really interested in.

  24. #24
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff View Post
    I'm taking the view that hints are simply documentation and documentation is unecessary clutter which should be removed in favour of more expressive naming. There are exceptions eg dependency injection tool like Phemto.
    Thanks for your kind response.

    I of course agree that naming is very important but naming is essentially magic...or at best, convention. Moreso, typehints do more than just mere documentation -- to be sure, they enforce a code behaviour (ie: engine level checking) that is otherwise relegated to capriciousness of the user.

    I'll also agree that typehinting everything, everywhere would be overkill in my view; still, I can't see the merit in hand-waving away interfacing and typehinting with the suggestion that it is somehow a form of code clutter. If anything, it should tend to reduce code-clutter by helping reduce the number of checks one would otherwise want to make to verify passed in objects, not?

    At any rate, I'll assume that we are both somewhat set in our ways on this matter and I won't try to push the debate any further.

    Best regards.
    Last edited by jayboots; Dec 11, 2006 at 14:52. Reason: fixed paragraph mixup

  25. #25
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jayboots View Post
    it should tend to reduce code-clutter by helping reduce the number of checks one would otherwise want to make to verify passed in objects, not?
    I would suggest instead "replace run-time checks with unit/integration tests" (I don't know if that's a recognised refactoring but it should be in there somewhere). In a properly tested app I can assume that the rest of the code behaves correctly. The tests are where interfaces are specified and this is where I'd catch any problems with incorrect types being passed around. It's very, very rare that I need to check type in the code. Test driven design is all about expressing real requirements clearly and then writing the minumum code necessary to fulfil them. Extra complexity of any kind is going to make refactoring harder. If tests pass without hints I would have no reason to add them.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •