Question

From a quick Google search and a the wikipedia article on Multiple Inheritance, which quotes:

Multiple inheritance refers to a feature of some object-oriented programming languages in which a class can inherit behaviors and features from more than one superclass. This contrasts with single inheritance, where a class may inherit from at most one superclass.

I understand that PHP doesn't allow multiple inheritance. What I can't find a clear answer on, however, is whether it allows more than one class to extend a superclass. Example:

class a {}
class b extends a {}
class c extends a {}

In terms of what I'm trying to do, I'm making an RPG and want a 'generic' character class to include all the methods and properties that make the template of a character. Then I want classes to include specifics for each type of character (warrior, mage, etc), such as stats modifiers and special attacks.

Is this possible?

Was it helpful?

Solution

Yes, it's perfectly possible. The entire purpose of inheritance is that multiple children can inherit common functionality from a parent.

OTHER TIPS

Yes, multiple classes can extend the same class, per your code example.

This is the foundation of OOP. It's possible.

Yes, any class can extend a base class. In your example, it just isn't possible for class c to extend both a and b.

So in an adjusted example like the one above

class a {}
class b extends a {}
class c extends b {}

c could not get to properties of a ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top