120

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*
2
  • 19
    How is this too localized?! "This question is unlikely to help any future visitors". Yet the best answer has 33 upvotes and the question 4 stars!
    – duality_
    Commented Feb 27, 2013 at 23:34
  • @duality_ 186 upvotes best answer, 103 upvotes question and 18 stars. You just got to love StackOverflow. Commented Jun 17, 2021 at 17:46

3 Answers 3

211

Try it the other way around:

class database extends mysqli implements databaseInterface { ...}

This should work.

3
  • Ahh, thank you very much. Why exactly is it that they should be the other way around?
    – Pim Jager
    Commented Mar 16, 2009 at 21:15
  • 17
    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
    Commented Mar 16, 2009 at 21:18
  • Is it a bad practice?
    – Shamshid
    Commented Feb 28, 2019 at 9:49
28

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.

6
  • 1
    Why is extending the mysqli class a bad idea?
    – Pim Jager
    Commented Mar 16, 2009 at 21:17
  • 13
    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. Commented Mar 16, 2009 at 21:25
  • 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. Commented Mar 16, 2009 at 21:35
  • 2
    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. Commented Mar 16, 2009 at 21:48
  • 2
    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. Commented Mar 16, 2009 at 22:32
12

yes, in fact if you want to implement multiple interfaces you can do like this:

public class MyClass extends BaseClass implements myInterface1, myInterface2, myInterface3{ 

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.