Last active
June 21, 2016 00:06
-
-
Save duan-li/b53a2a1b512447797c3bdc2e47a1d131 to your computer and use it in GitHub Desktop.
fake multiple inheritance, use the magic function __call().
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ref: http://stackoverflow.com/questions/356128/can-i-extend-a-class-using-more-than-1-class-in-php | |
class B { | |
public function method_from_b($s) { | |
echo $s; | |
} | |
} | |
class C { | |
public function method_from_c($s) { | |
echo $s; | |
} | |
} | |
class A extends B | |
{ | |
private $c; | |
public function __construct() | |
{ | |
$this->c = new C; | |
} | |
// fake "extends C" using magic function | |
public function __call($method, $args) | |
{ | |
$this->c->$method($args[0]); | |
} | |
} | |
$a = new A; | |
$a->method_from_b("abc"); | |
$a->method_from_c("def"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment