Page 1 of 1

Implementing an Interface with an Abstract Class - Problem

Posted: Tue Jan 19, 2010 10:55 am
by kerryb
Hello,

I'm trying to implement an Interface with an abstract class but for some reason I'm getting an error (although no error is being shown, the class is just not being created.

Here is the interface :

Code: Select all

interface Document {
 
    public function setDocument();
    public function getWatermarkedDoc();
    public function getPreview();
    public function setTile();
    public function getTile();
    public function setTilePosition($position);
    public function getTilePosition();
}
The Abstract class :

Code: Select all

abstract class AbstractDocument implements Document{
 
    abstract public function setDocument();
    abstract public function getWatermarkedDoc();
    abstract public function getPreview();
    public function setTile(){
 
    }
    public function getTile(){
 
    }
    public function setTilePosition($position){
 
    }
    public function getTilePosition(){
 
    }
}
And finally the concrete class :

Code: Select all

class ImageDocument extends AbstractDocument{
 
    public function __construct(){
        echo "Hello 2";
    }
    public function setDocument(){
 
    }
    public function getWatermarkedDoc(){
 
    }
    public function getPreview(){
 
    }
}
It doesn't seem to be working properly as there is no "hello 2" printed when I instantiate ImageDocument.

Am i right in think I can implement an Interface with an abstract class ?

Thanks for your advice.

Re: Implementing an Interface with an Abstract Class - Problem

Posted: Tue Jan 19, 2010 12:52 pm
by requinix
You should be getting fatal errors: "can't inherit abstract function Document::X() (previously declared abstract in AbstractDocument)" where X is one of the abstract functions.

It's kinda a bug in PHP in that it's not giving you a good error message, but once you understand how OOP in PHP works the message makes sense.

Methods defined in interfaces are essentially abstract methods in an abstract class. When you defined setDocument in both Document and AbstractDocument you defined two abstract methods: that's a conflict and is not allowed.
If setDocument, getWatermarkedDoc, and getPreview have no implementation in AbstractDocument then you should not define them again.

Code: Select all

abstract class AbstractDocument implements Document{
 
    public function setTile(){
 
    }
    public function getTile(){
 
    }
    public function setTilePosition($position){
 
    }
    public function getTilePosition(){
 
    }
}