1

Assuming I have 2 classes

Class A {
    public function doA(){
        echo "Im A";
    }
}
Class B {
    public function doB(){
        echo "Im B";
    }
}

write Class C, in order that the following code runs:

  $c = new C();
  $c->doA();
  $c->doB();

and outputs:

>> Im A
>> Im B

This was in a test, and the conditions where:

  • use no static calls
  • you can't modify class A or class B

so I wrote:

Class C {

       public function doA() {
             $a = new A();
             $a->doA();
       }

       public function doB() {
             $b = new B();
             $b->doB();
       }

  }

So apparently I was wrong as it can be "more optimized"

can someone tell me how to do it?

3
  • 1
    class C extends A,B{}? Commented Jan 24, 2014 at 17:00
  • 3
    @RocketHazmat you can't do that in PHP. Commented Jan 24, 2014 at 17:01
  • @MatteoTassinari: Never mind, then. :-) I knew you could implement multiple things, I thought you could extend multiple things too. Guess you can't. Commented Jan 24, 2014 at 17:01

3 Answers 3

5

You could keep instances of A and B instead of instantiating them each time.

class C {
       private $a, $b;

       public __construct() {
             $this->a = new A();
             $this->b = new B();
       }

       public function doA() {
             $this->a->doA();
       }

       public function doB() {
             $this->b->doB();
       }
}
Sign up to request clarification or add additional context in comments.

1 Comment

wooooow nice and easy... :D
2

PHP has no "native" multiple inheritance, but you can achieve something similar to it by using traits.

Trait A {
    public function doA(){
        echo "Im A";
    }
}

Trait B {
    public function doB(){
        echo "Im B";
    }
}

Class C {
    use A, B;
}

$c = new C;
$c->doA();
$c->doB();

Note that this would require at least PHP 5.4.

7 Comments

I was not allowed to modify Class A or Class B
Then go with @rid answer.
He can't modify classes A and B, and that's not really the ideal way to use traits.
Why not, it is just an example though.
Because it was a test, and those were the rules specified.
|
1

To do it without modifying classes, the best and optimised option would be as follows.

class C {
    private $a;
    private $b;

    public __construct() {
        $this->a = new A();
        $this->b = new B();
    }

    public function __call($method, $arguments = array()) {
        if(method_exists($this->as, $method)) {
            return call_user_func(array($this->a, $method));
        }
    }
}

The above is also future proof, so adding new methods would also follow.

While you were told not to modify classes A and B, the correct way to do this would be by having B extend A, then having C extend B, like below.

class A {
    public function doA(){
        echo "Im A";
    }
}
class B extends A {
    public function doB(){
        echo "Im B";
    }
}
class C extends B {

}

$c = new C();
$c->doA();
$c->doB();

6 Comments

I'm not sure if that's the "correct" way. It's a way, with advantages and disadvantages (just as __call is not necessarily "the" way, but just a way, again with advantages and disadvantages).
Tbh, the question is a rather broad one. Faced with the situation I'd edit classes A & B. There's no reason to not.
well, it was a job interview xD and I got rejected mustly for doing the cascade inheritance thing :(
Sure there is. One big disadvantage is that you'd add a dependency, making B depend on A even if it doesn't have to. Another disadvantage is that you'd get a deeper inheritance tree, making maintenance harder in the long run (the __call approach is much bigger maintenance issue though). The only advantage I see is that you write less code. Again, each way of doing this has advantages and disadvantages, and there's always a reason not to do it one way or another.
The problem with job interview tests is that they often pose you with problems you rarely face.
|

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.