Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

oop

php

extends

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.

like image 871
Brett Avatar asked Jul 03 '11 20:07

Brett


People also ask

Is it possible to extend 2 classes together?

You can't extend two or more classes at one time. Multiple inheritance is not allowed in java.

Can multiple classes extend the same class?

It means a class can extend only a single class at a time. Extending more than one class will lead to code execution failure. When a class extends a class, then it is called single inheritance . If a class extends more than one class, it is called multi-inheritance , which is not allowed in Java.

Can I extend more than 1 class in Java?

Java does not allow extending multiple classes.

Can I extend 2 classes in PHP?

Classes, case classes, objects, and traits can all extend no more than one class but can extend multiple traits at the same time.


2 Answers

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
like image 195
alxbl Avatar answered Sep 22 '22 09:09

alxbl


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();
like image 40
RReverser Avatar answered Sep 20 '22 09:09

RReverser