Skip to content

Instantly share code, notes, and snippets.

@BEcraft
Created February 23, 2019 14:31
Show Gist options
  • Save BEcraft/7821027e31fd7cbfbcf0536683e5a184 to your computer and use it in GitHub Desktop.
Save BEcraft/7821027e31fd7cbfbcf0536683e5a184 to your computer and use it in GitHub Desktop.
<?php
class A {
private $e;
public function __construct(){
$this->e = new B();
}
public function __call(string $name, array $arguments) {
if (!method_exists($this, $name)) {
if (method_exists($this->e, $name)) {
return $this->e->$name($arguments);
}
throw new \Error("Call to undefined function \"{$name}()\" (#" . count($arguments) . ")");
} else {
return $this->$name($arguments);
}
}
public function e() {
return __CLASS__ . " : E";
}
}
class B {
public function f() {
return __CLASS__ . " : F";
}
}
$a = new A();
echo $a->f(); //prints 'B : F'
echo $a->z(); //throws error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment