informing interface methods are implemented via __call? informing interface methods are implemented via __call? laravel laravel

informing interface methods are implemented via __call?


Although there may be a larger question as to the cleanliness of such a design, you can accomplish something akin to this by using a trait which implements the methods of the interface:

interface FindableContract {    public function find($id);}trait MagicFindableTrait {    public function find($id) {        return static::__call(__FUNCTION__, func_get_args());     }}class MagicalParent {    public function __call($method, $args) {        if ($method == 'find') {            return "User " . $args[0] . " is a witch! May we burn her?!";        }    }}class User extends MagicalParent implements FindableContract {    use MagicFindableTrait;}class NonmagicalUser implements FindableContract {    public function find($id) {        return "User $id was found to be non-magical.  Let's burn him anyway.";    }}print (new User)->find(123);print (new NonmagicalUser)->find(321);


No this will not work. While __call() is really nice for a dynamic coding style it's disadvantages are that you cannot enforce the signatures of the dynamic methods in an interface and you won't get an automated documentation for it.

But I think if you are at a point where you want to create an interface for those methods, there should be no need to use __call() anymore. I would just hardcode the methods.