Skip to content

Instantly share code, notes, and snippets.

@duan-li
Last active June 21, 2016 00:06
Show Gist options
  • Save duan-li/b53a2a1b512447797c3bdc2e47a1d131 to your computer and use it in GitHub Desktop.
Save duan-li/b53a2a1b512447797c3bdc2e47a1d131 to your computer and use it in GitHub Desktop.
fake multiple inheritance, use the magic function __call().
<?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