Can you extend two classes in one class? [duplicate]
Asked Answered
G

5

10

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.

Glory answered 3/7, 2011 at 20:8 Comment(0)
P
17

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
Perch answered 3/7, 2011 at 20:13 Comment(3)
Although as others have said, a class cannot inherit from two classes at once (i.e. extends A, B or something)Perch
That's what I was thinking of doing but then I didn't think it would inherit stuff from the main parent. In this case which class is the parent? How do you access functions/methods from the non-parent?Glory
From Your_Class you can just call them using $this->method() as usual... every protected and public member will be inherited. Child is the parent of Your_Class but by polymorphism a type-hint of Parent will accept a Your_Class object as well. I added a bit of detail to the code example to show thatPerch
D
11

As said @morphles, this feature will be available as traits (like mixins in other languages) in php 5.4. However, if it's really needed, you can use such workaround:

// your class #1
class A {
    function smthA() { echo 'A'; }
}

// your class #2
class B {
    function smthB() { echo 'B'; }
}

// composer class
class ComposeAB {
    // list of implemented classes
    private $classes = array('A', 'B');
    // storage for objects of classes
    private $objects = array();

    // creating all objects
    function __construct() {
        foreach($this->classes as $className)
            $this->objects[] = new $className;
    }

    // looking for class method in all the objects
    function __call($method, $args) {
        foreach($this->objects as $object) {
            $callback = array($object, $method);
            if(is_callable($callback))
                return call_user_func_array($callback, $args);
        }
    }
}

$ab = new ComposeAB;
$ab->smthA();
$ab->smthB();
Deerdre answered 3/7, 2011 at 20:35 Comment(5)
You're welcome) Write me if you can't change it for your needs.Deerdre
Would this work if ComposeAB needed to implement an interface that specifies functions in A and B? Imagine if the interface had smthA and smthB, ComposeAB implemented such an interface, would the __call function satisfy the requirements of the interface?Pronunciamento
@Pronunciamento Nope. bugs.php.net/bug.php?id=41162Deerdre
@Pronunciamento But in new PHP you can you traits for better multiple inheritance.Deerdre
Ah so I would just have to redeclare all the functions.Pronunciamento
B
8

No php is single inheritance language. If you can you can lookup upcoming feature in php 5.4, that is traits.

Bearnard answered 3/7, 2011 at 20:11 Comment(0)
C
2

As a yes/no answer i can say no. You can't extend from multiple classes in php. but you can use interface instead.

Cash answered 3/7, 2011 at 20:12 Comment(0)
P
0

You can do this by thinking backwards, however it depends on the situation. If you would like to keep your classes separate, nobody can give you a good answer of how to structure your objects and inheritance unless you give us more information. Also keep in mind that getting too worried about getting class structure right is a burden in small projects and you may as well just move the methods over and be done with it and learn more about classes later.

Prelature answered 3/7, 2011 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.