I have an SystemInfoFactory class in my application which has a getSystemInfo() method:
/**
* Returns SystemInfo object based on which OS
* server uses
*
* @return SystemInfoInterface SystemInfo object
*/
public function getSystemInfo()
{
$os = $this->getOS();
$systemInfo = null;
switch ($os) {
case "Linux":
$systemInfo = new LinuxInfo();
break;
case "Darwin":
$systemInfo = new OSXInfo();
break;
case "Windows":
$systemInfo = new WindowsInfo();
break;
}
return $systemInfo;
}
So it chooses appropriate object according to host system. Now, each "info class" implements SystemInfo interface (methods like getArchitecture, getCPU etc.), but as you see, nowhere in my code is it checked whether returned object really implements interface. Would it be considered "good practice" to check if selected $systemInfo object implements it before returning it? It obviously isn't required, but in case somebody extends this application (adds BSD support for example) and forgets to implement all methods it might be harder for him to debug.
implements SystemInfoInterfacein his class and the code will work without problems.undefined methoderror at some point, but the real problem source could be buried deep in call stack.