One way of doing this would be to create an abstract class 'Challenge', and have a 'LeagueChallenge' and 'TournamentChallenge' that extend this abstraction; however, I've been told that this wouldn't be possible with Laravel.
According to your question given above you can use an abstract class and can extend
it when creating sub-classes but if you need to use Eloquent
by extending the Eloquent
model then you can't extend two classes at the same time and it's a limitation in PHP
not for Laravel
, but you may use a different approach.
Just create a Base
class by extending the Eloquent
like this:
// Challenge Model
abstract class Challenge extends Eloquent {
// Force Extending class to define this method
abstract public function someMethod();
}
Then create sub-classes by extending the Challenge
class like this:
// LeagueChallenge Model
class LeagueChallenge extends Challenge {
public function someMethod()
{
//...
}
}
// TournamentChallenge Model
class TournamentChallenge extends Challenge {
public function someMethod()
{
//...
}
}
You may use repository pattern in your application, it will help you to maintain and test your application easily but you are not bound to use this.