I've been using a somewhat odd yet effective pattern for a current use case. The one issue is that I'm getting an undefined method notice on a method that is unique to the subclass. The method of course works fine but the notice leads me to believe that either my architecture is shotty or something else is going on.
I don't believe this violates Liskov Principle as the subclass satisfies all contracts with identical signatures, put simply, it could replace any other subclass and work reliably.
Is this an instance of the variance issue PHP has stated when releasing type declarations, a violation of Liskov principle, or do I need to clean up my architecture?
Quick code example and brief explanation.
interface Quiz
{
public function generate() : void;
}
class LessonQuiz implements Quiz
{
public function generate() : void
{
//stuff here
}
public function lessonId() : int
{
//stuff here
}
}
class QuizClient //Determines/returns instantiated quiz subclass & other permission stuff.
{
public static function create(string $case) : Quiz
{
switch($case){
case('lesson'): return new LessonQuiz;
//more logic
}
}
}
/** In another class that builds a lesson template.
* ...
*/
$quiz = QuizClient::create('lesson');
$lessonId = $quiz->lessonId(); //undefined method.
I have a client that determines and returns a subclass object of an interface. The return type is set to the interface as I need to have some instance of that interface returned.
All subclasses satisfy the interface contracts with identical signatures. However, the subclass specific method is undefined. I don't understand why this is incorrect. Class 'LessonQuiz' is an instance of 'QuizFactory' but also 'LessonQuiz'.
This does work but the fact that it reports method undefined makes me suspicious about my architecture.
Would really appreciate any help.