informing interface methods are implemented via __call?

柔情痞子 提交于 2019-12-08 16:37:49

问题


I have an interface that declares the implementation needs methods such as find, findOrFail etc, basically Laravel eloquent methods.

I declare these methods in the interface because not everything that implements the interface will extend eloquent so I declare them in the interface so my app always knows the methods are going to be there.

What I want to know is, other than having a bunch of public function find($id){return parent::find($id)} type methods in the models that do extend the eloquent model is there an easy way to let the interface know that the method is handled via __call?


回答1:


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 FindableTrait;
}

class NonmagicalUser implements FindableContract {
    public function find($id) {
        return "User $id was found to be nonmagical.  Let's burn him anyway.";
    }
}

print (new User)->find(123);
print (new NonmagicalUser)->find(321);



回答2:


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.



来源:https://stackoverflow.com/questions/17267064/informing-interface-methods-are-implemented-via-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!