Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

informing interface methods are implemented via __call?

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?

like image 611
Hailwood Avatar asked Jun 24 '13 01:06

Hailwood


1 Answers

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);
like image 181
Gabriel Avatar answered Nov 04 '22 09:11

Gabriel