In PHP, you cannot "force" a method to return anything -- and it's not possible, even with abstract classes/methods, nor interfaces.
The best you can do is indicate that the implementation should return something, using a comment -- but you cannot force people to do so :
/**
* @param string $a blah blah
* @return int The return value blah blah
*/
public function my_method($a);
Of course, if you are calling this method (the implementation) from your framework, you can check what has been returned, and throw an Exception if it doesn't correspond to what you expected...
And here is a quick example of how this could be implemented :
class ClassA {
/**
* @param string $a blah blah
* @return ClassB The return value blah blah
*/
public function my_method($a) {
echo 'blah';
}
}
class ClassB {
// ...
}
$a = new ClassA();
$returned = $a->my_method(10);
if (!$returned instanceof ClassB) {
throw new Exception("Should have returned an instance of ClassB !");
}
Here, as the method doesn't return an instance of ClassB, the exception will be thrown.