I understand that my question is somehow wrong, but I'm still trying to solve this problem.
I have an interface Programmer:
interface Programmer {
public function writeCode();
}
and a couple of namespaced classes:
Students\BjarneProgrammer(implementsProgrammer)Students\CharlieActor(implementsActor)
I have this class names stored in array $students = array("BjarneProgrammer", "CharlieActor");
I want to write a function, that will return an instance of class if it's implementing Programmer interface.
Examples:
getStudentObject($students[0]); - It should return an instance of BjarneProgrammer because it's implementing Programmer.
getStudentObject($students[1]); - It should return false because Charlie is not a Programmer.
I tried it using instanceof operator, but the main problem is that I do not want to instantiate an object if it's not implementing Programmer.
I checked How to load php code dynamically and check if classes implement interface, but there is no appropriate answer as I don't want to create object unless it's returned by function.