Reputation: 1408
I want to use interfaces, but some of my implementations rely on magic methods such as __invoke and __call. I have to remove signatures of methods that may be invoked magically (in any implementation) from the interface. Which leads to the anti pattern Empty Interface (Yeah, I just made that up).
How do you combine interfaces and magic methods in PHP?
Upvotes: 18
Views: 5550
Reputation: 36562
Have all of the interface methods in your implementations dispatch to __call()
. It involves a lot of crummy cut-and-paste work, but it works.
interface Adder {
public function add($x, $y);
}
class Calculator implements Adder {
public function add($x, $y) {
return $this->__call(__FUNCTION__, func_get_args());
}
public function __call($method, $args) {
...
}
}
At least the body of each method can be identical. ;)
Upvotes: 16