Can you extend two classes in one class?

Brett picture Brett · Jul 3, 2011 · Viewed 35.9k times · Source

Possible Duplicate:
Can I extend a class using more than 1 class in PHP?

I have a class that has several child classes, however I am now adding another class which I also want to be a child of the parent, however I would also like to utilize many of the functions from one of the other child classes.

I've thought of just moving the respective functions from the other child class to the parent, however didn't think this was really needed as it would only be these two child classes that make use of them so was hoping that I could extend from the main parent class and from one of the existing child classes.

Answer

alxbl picture alxbl · Jul 3, 2011

You can extend the child class to inherit both its parent and the child's functions, which I think is what you are trying to do.

class Parent
{
    protected function _doStuff();
}

class Child extends Parent
{
    protected function _doChildStuff();
}

class Your_Class extends Child
{
    // Access to all of Parent and all of Child's members
}

// Your_Class has access to both _doStuff() and _doChildStuff() by inheritance