Skip to content

Instantly share code, notes, and snippets.

@egulhan
Created May 16, 2014 13:15
Show Gist options
  • Save egulhan/4abde2d66dd041b39f0e to your computer and use it in GitHub Desktop.
Save egulhan/4abde2d66dd041b39f0e to your computer and use it in GitHub Desktop.
PHP does not support multiple inheritance. This shows how to make it possible.
// Source: http://stackoverflow.com/a/356431
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");
@egulhan
Copy link
Author

egulhan commented Jan 30, 2015

@xeoncross, yes you are right. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment