Consider the following class definitions:
class SuperDate {}
class SubDate extends SuperDate {}
class Foo
{
public function setDate(SubDate $date) {}
}
class Bar extends Foo
{
public function setDate(SuperDate $date){}
}
$foo = new Foo();
$bar = new Bar();
$bar->setDate(new SubDate());
$foo->setDate(new SubDate());
This code gives the following error:
Strict standards: Declaration of Bar::setDate() should be compatible with Foo::setDate(SubDate $date)
Obviously, the reason is that the signature of Bar::setDate
is different from that of Foo:setDate
. In StackOverflow in English there is a similar question that refers a violation of the Principle of Substitution of Liskov as the cause of the "strict warning ", but the situation is slightly different because the subclass is more restrictive than the parent class.
However, in my case, the signature in Bar is more "wide" than in Foo and therefore completely interchangeable. That is, since Bar is a subtype of Foo, then objects of type Foo can be replaced by objects of type Bar without having to change the properties of the program.
So, my question is: Why does this code violate "Strict Standards"?