I am using some database polymorphism to connect table relations through eloquent/laravel. (NOTE the example below has been simplified, the two objects are already extending a base class so I can not extend another that would implement the behavior I want, the baseclass they extend is shared with other classes which are unrelated to what I'm trying to do below)
class Pageable{
public function table() {
return $this->morphTo();
}
}
class User {
public function pageable() {
return $this->morphOne('Pageable', 'table');
}
public function returnUrl() {
return "http://...";
}
}
class Company {
public function pageable() {
return $this->morphOne('Pageable', 'table');
}
public function returnUrl() {
return "http://...";
}
}
I iterate through pageable and can then retrieve either a User or Company object, however I need a generic function such as returnUrl that I can call on any of the objects that might be polymorphed into that relation. Is there any way to clean this up above or at least create some fail safe checks to ensure all the required method are implemented in the class if for instance they implement the PeageableInterface. For instance could I set in the interface a requirement that this returnUrl function is required?
How would I create such interface?