For developing a little framework I tried to implement interfaces dynamically. But the idea to use the reflection api and the magic method __get does not work. It is not invoked, the compiler breaks before with:
Fatal error: Class Test contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods
interface IA {
public function getA();
}
interface IB {
public function getB();
}
class Aimpl {
public function getA() {
echo "A";
}
}
class Bimpl {
public function getB(){
echo "B";
}
}
class Test implements IA, IB {
public function __call($name, $arguments) {
$class = new ReflectionClass($this);
$interfaces = $class->getInterfaces();
$found = false;
foreach ($interfaces as $iname => $interface) {
$methods = $interface->getMethods();
foreach ($methods as $method) {
if($name == $method->name) {
$found = true;
// find the implementation
}
}
}
if(!$found) {
throw new Exception("Method not found");
}
}
}
$test = new Test();
$test->getA();
No comments:
Post a Comment