tags:

views:

1536

answers:

2

Can a class extend both an interface and another class in PHP?
Basically I want to do this:

interface databaseInterface{
 public function query($q);
 public function escape($s);
 //more methods
}

class database{ //extends both mysqli and implements databaseInterface
 //etc.
}

How would one do this, simply doing:

class database implements databaseInterface extends mysqli{

results in a fatal error:

Parse error: syntax error, unexpected T_EXTENDS, expecting '{' in *file* on line *line*
+6  A: 

Try it the other way around:

class database extends mysqli implements databaseInterface { ...}

This should work.

Simon Lehmann
Ahh, thank you very much. Why exactly is it that they should be the other way around?
Pim Jager
Since you can only inherit one class and you can implement any number of interfaces, it was probably a syntax sugar thing. Also, remember, if your subclass implements any interfaces, that would show up in the list of implemented methods too. So putting them in that order makes certain sense.
Drew
+1  A: 

Yes it can. You just need to retain the correct order.

class database extends mysqli implements databaseInterface { ... }

Moreover, a class can implement more than one interface. Just separate 'em with commas.

However, I feel obliged to warn you that extending mysqli class is incredibly bad idea. Inheritance per se is probably the most overrated and misused concept in object oriented programming.

Instead I'd advise doing db-related stuff the mysqli way (or PDO way).

Plus, a minor thing, but naming conventions do matter. Your class database seems more general then mysqli, therefore it suggests that the latter inherits from database and not the way around.

Michał Rudnicki
Why is extending the mysqli class a bad idea?
Pim Jager
First, because it's not yours. When mysqli guys decide to change something subclass's behaviour will also change. Second, inheritance is used to SPECIALIZE, not EXTEND with extra functionality. This is the most misunderstood thing in OOP. As a rule of thumb I tend to extend abstract classes only.
Michał Rudnicki
Michal - how would a change to mysqli be any different for a developer who use a polymorphic approach vs an aggregated/composed one? You'd still have to update your code to handle the changes.
Peter Bailey
Peter, it may seem exactly the same, but it's not. By definition if you use composition rather than inheritance you create adapter (google for adapter pattern). This way you loosen dependency and can make adapter immune to changes. But then, why not just use mysqli the way it was intended to be?
Michał Rudnicki
I'm familiar with the adapter pattern. I'm just saying that if a method name changes or is deprecated, you'll still need to change code in both scenarios. While I agree that a non-polymorphic approach is better in this scenario, I don't buy the notion that an adapter would be "immune" to change.
Peter Bailey
You will only have to change adapter code, not all places using that code. With composition you can provide compatibility layer no matter what, whereas with inheritance you can call it at best a hack. And, I stand corrected, adapter would provide better immunity to change.
Michał Rudnicki