2

In PHP, is it possible to have multiple inheritance (by the nature of the PHP, not writting modification code)?

For example :

class a
{
    public function foo();
}

class b
{
    public function bar();
}

class c extends a, b
{
    public function baz();
}
4
  • Here's a good answer Commented Apr 8, 2010 at 19:07
  • 1
    possible duplicate of stackoverflow.com/questions/90982/multiple-inheritance-in-php Commented Apr 8, 2010 at 19:08
  • Might be OT but you can implement multiple interfaces Commented Apr 8, 2010 at 19:44
  • And can an interface define a method? I need to have debug methods in all my classes. I also have a DBUser class I want to inherit to provide DB helper functions for classes. Commented Apr 12, 2014 at 10:59

2 Answers 2

1

No. There's no real multiple inheritance in PHP and thats a good thing. See the other answers for alternatives.

Edit: By now, PHP supports Traits, of which one class can include more than one. They avoid the usual problems of multiple inheritance by throwing an error or requiring you to alias conflicting names.

Sign up to request clarification or add additional context in comments.

3 Comments

@Inwdr I don't know if that's an issue as other languages have managed it somehow ... anyway, doesnt matter
Many other languages have managed it the same way as PHP, they simply don't allow multiple inheritance.
0
class A extends B{

}

class B extends C{

}

class C{

}

1 Comment

This isn't multiple inheritance. It's a subclass of a subclass.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.