SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru Galo's Avatar
    Join Date
    May 2005
    Location
    Holland!
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Abstract Interfacing

    Hi gang,

    I have some doubt's around the use of Abstract Interfaces.
    With Abstract Interfaces i mean Interfaces that are beeing implemented by an Abstract class to provide the Concrete classes with the methods the Interface states.

    Code:
    interface Foo{
          abstract function Bar();
    }
    
    abstract class FooBar implements Bar{}
    
    class VarFoo extends FooBar{
          public function Bar(){
                //Foo
          }
    }
    The point is this feels the right thing to do, but im not sure if this is a common approach. This also looks a bit like a strategy pattern to me but im not sure.

    Im implementing this structure in a home grown Framework im working on, but im not sure if this is the right way of using interfaces for abstract classes.

    IMHO, by making the Methods in the Interface Abstract when implementing it into an abstract class it does not require the Methods cause this is an Abstract v/s Abstract situation, however when i extend the Abstract class you automaticly get an exception when the Abstract methods from the Interface are not declared, and this is why it feels right, but im not sure why i have doubts about implementing the Abstract methods in the Abstract class afterall.

    My question may be a little odd to you but i really like to hear your comments on how this is done in other applications.

    I studied severall Frameworks out there but they don't work with Interfaces probably cause they were upgraded from PHP4.x which does not support Interfaces.

    I hope we could disscuss this since i've seen more people struggle with Interfaces at this board and this seems to be a common issue.

    Yours,
    Galo
    Business as usual is off the menu folks, ...

  2. #2
    SitePoint Enthusiast matid's Avatar
    Join Date
    May 2005
    Location
    Knurow, Poland
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interface's methods must be public. That's the nature of interface. When some class implements the interface you're 100% sure that you can comunicate with it uses methods declared in the interface. If the class is declared abstract you cannot comunicate with it (you cannot even create an object) as interfaces ensures.

  3. #3
    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 matid
    Interface's methods must be public. That's the nature of interface. When some class implements the interface you're 100% sure that you can comunicate with it uses methods declared in the interface. If the class is declared abstract you cannot comunicate with it (you cannot even create an object) as interfaces ensures.
    That's exacly what im trying to say, you can declare the methods abstract in the interface so when implementing it in an abstract class you don't have to define them in the abstract class but rather define them in your concrete class.

    They don't HAVE to be public, eventualy they will be public in your concrete class but that does not mean you can't define them abstract in you'r interface
    Business as usual is off the menu folks, ...

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so when would you use an abstract base class instead of an interface, or vice versa?

  5. #5
    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 skinny monkey
    so when would you use an abstract base class instead of an interface, or vice versa?
    Not, Abstract base classes (like the word says) provide you with a Base to extend on, if you would implement an Interface which contains abstract methods into the Abstract Base class, and extend that Base class that is your concrete class which will then have to be defining the methods that were declared by the Interface.

    So when would i use an abstract base class, i always throw them in when i feel i have to provide my system with an overall selection of methods but i have more then one target that is using these methods in different ways.

    Like if you have a Abstract Collection Base class, you coulod now easely extend the abstract class by a concrete File class that implements it;s methods like Add, Remove, Fetch etc...
    You can ADD a record to a collection but also can you ADD a record to a databaser or a file to a directory, having a Base class which implements an interface with abstract methods alows me to implement different functionality by re-using my Abstract base class.
    Business as usual is off the menu folks, ...

  6. #6
    SitePoint Addict timvw's Avatar
    Join Date
    Jan 2005
    Location
    Belgium
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An abstract class and an interface have something in common: They both expose a collection of public methods. So, if you have a class that extends / implements them you are sure you will be able to use those methods on that class.

    The difference between an interfact and an abstract class is that an abstract class can contain implementation (For example it can already implement a public function, or some protected functions that can help you to implement such a public function).

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I might point out that your class methods you declare in an interface must be public, they cannot be abstract, public or otherwise

    PHP Code:
    interface IComposite {
    public 
    functioni getId();
    public function 
    hasChildren();
    public function 
    getChildren();
    // throws an error
    abstract public function attachIComposite $composite );

    As an example

  8. #8
    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 Dr Livingston
    I might point out that your class methods you declare in an interface must be public, they cannot be abstract, public or otherwise

    PHP Code:
    interface IComposite {
    public 
    functioni getId();
    public function 
    hasChildren();
    public function 
    getChildren();
    // throws an error
    abstract public function attachIComposite $composite );

    As an example
    It seems i was way ahead of myself, evetualy you can implement a public method from an Interface into an abstract method without having to implement it, but this is where i get confused.

    See, when you define an Interface, you can define a method public or abstract. Although evetualy at implementation of that method you will have to state it public, when implementing an interface into an abstract class it does not generate the error

    "Fatal error: Class BarFoo contains 1 abstract methods and must therefore be declared abstract (Foo::Bar) in D:\public\foo.php"

    Whenever this class is being extended it generates the error, so this is confusing me...

    example :
    Code:
    <?php
    
    	interface Foo{
    		
    		abstract function Bar();
    	
    	}
    	
    	abstract class FooBar implements Foo{
    	
    		//I'm not required to put the method here now	
    	}
    	
    	/*
    	class BarFoo extends FooBar{
    		
    		//When i delete the comment i AM required to put the method here, so why not in the abstract base class then ?
    		
    	}
    	*/
    ?>
    Sorry, just a few issues this little grasshopper still hasn't masterd
    Business as usual is off the menu folks, ...

  9. #9
    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 timvw
    An abstract class and an interface have something in common: They both expose a collection of public methods. So, if you have a class that extends / implements them you are sure you will be able to use those methods on that class.

    The difference between an interfact and an abstract class is that an abstract class can contain implementation (For example it can already implement a public function, or some protected functions that can help you to implement such a public function).
    But isn't that the same as an Interface extending another interface, sound the same to me as an abstract class already implementing public methods no ?

    Dr.Livingston, you stated an abstract public method in your code which would generate an error, although i was aware of the fact that you cant declare something abstract and then declare it public it is posible to declare the method abstract in the interface and then declare it public in the concrete class, by using an abstract class to implement the interface im not forced to use it in the abstract class rather then to implement the method into an concrete class which extends the abstract base class, but this does not tell me what your point exacly is....

    edit : i have done the same as you an i dont get any errors....
    PHP Code:
    <?php

        
    interface Foo{
            
            abstract public function 
    Bar(Foo $oFoo);
        
        }
        
        abstract class 
    FooBar implements Foo{
        }
        
        
        class 
    BarFoo extends FooBar{
            
            public function 
    Bar(Foo $oFoo){}
            
        }
        
    ?>
    This just works fine, so i dont get your statement....

    thanks,
    Galo
    Business as usual is off the menu folks, ...

  10. #10
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Galo
    PHP Code:
    interface Foo{
        abstract function 
    Bar();

    As Dr. Livingston meant to point out, that's not allowed. An interface's methods are all abstract, so there's no need to redeclare it (which would generate a fatal error).
    Edit:

    This just works fine, so i dont get your statement....
    It does? Well, no matter. PHP is just being as lax as ever. There is no need to declare an interface's methods abstract, and it won't make a difference if you do.

    PHP Code:
    abstract class FooBar implements Foo{
        
    //I'm not required to put the method here now    
    }
    class 
    BarFoo extends FooBar{
        
    //When i delete the comment i AM required to put the method here, so why not in the abstract base class then ?

    That is because FooBar is abstract and cannot be instantiated. I'm quite sure this isn't such a difficult concept: interfaces are completely abstract classes. They can only define public methods' footprints. An abstract class may implement an interface to pass the requirement of adhering to the interface to its offspring (but it might as well not, and provide the implementation itself).

  11. #11
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no need to declare an interface's methods abstract, and it won't make a difference if you do.
    I agree. The point of an Interface is to make sure that a class(es) that implement that Interface, adhere to those class methods declared in the Interface. The reason you can only declare public class methods, is to allow the Interface to be implemented in one or more classes, as I see it, thus you are leaving out the implementation.

    Having this in an Interface,

    PHP Code:
    interface IComposite {
    abstract public function 
    attachIComposite $composite );

    To me, means that you are declaring an implementation, which one class which implements this Interface, has to implement that as well, which may not be the intentions. Remember that Interfaces are not there for the declaration of the implementation, intented or otherwise, it's the abstract class that intends the implementation

    I'm not sure the reasoning behind Galos attempt to abstract those class methods in an Interface, but this to me is a waste of time? If you have a class method which you know for a fact you wanted to be implemented in a given abstract class, then you put that class method in the said abstract class.

    And let the concrete over-ride it if required. On the other hand, you can have that given class method as an abstract class method in the abstract class, leaving out the implementation altogether.

    That is why we have abstraction in the first place,... The concrete class has to have the implementation (of the abstracted class method), and so, this makes this,

    PHP Code:
    interface IComposite {
    abstract public function 
    attachIComposite $composite );

    Redundant, doesn't it? I think so in that regard
    Last edited by Dr Livingston; Aug 8, 2005 at 11:59. Reason: add more clarity

  12. #12
    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 Ezku
    As Dr. Livingston meant to point out, that's not allowed. An interface's methods are all abstract, so there's no need to redeclare it (which would generate a fatal error).
    Edit:

    It does? Well, no matter. PHP is just being as lax as ever. There is no need to declare an interface's methods abstract, and it won't make a difference if you do.

    That is because FooBar is abstract and cannot be instantiated. I'm quite sure this isn't such a difficult concept: interfaces are completely abstract classes. They can only define public methods' footprints. An abstract class may implement an interface to pass the requirement of adhering to the interface to its offspring (but it might as well not, and provide the implementation itself).
    "It does? Well, no matter. PHP is just being as lax as ever. There is no need to declare an interface's methods abstract, and it won't make a difference if you do."

    Yeah, makes people like me ask questions like this

    Anyway, i didn't knew that i thought it was posible to do this in any language so this now is clear to me... thanks
    Business as usual is off the menu folks, ...


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
  •