Parent class method always throws, child overrides and returns—return type mismatch #14898
|
I have the following setup: abstract class Foo
{
/**
* @return never
*/
public function bar()
{
throw new Exception();
}
}
class Bar extends Foo
{
#[\Override]
public function bar()
{
return 'baz';
}
}I firmly believe, that the annotation of |
Answered by
ondrejmirtes
Jul 2, 2026
Replies: 1 comment 4 replies
|
They don't fit together. Return types must be covariant, parameter type contravariant. The contract of Foo::bar is that it always throws. Bar::bar violates that. |
4 replies
Answer selected by
shaedrich
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
They don't fit together. Return types must be covariant, parameter type contravariant.
The contract of Foo::bar is that it always throws. Bar::bar violates that.