5
take into account the following definitions of classes:
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 cause is the signature of Bar::setDate
be different from Foo:setDate
. Stackoverflow in English has a similar question which refers to a violation of The Liskov Substitution Principle as the cause of "Strict Warning", but the situation is slightly different because the subclass is more restrictive than the mother class.
However, in my case, the subscription in Bar is more "wide" than in Foo and therefore completely interchangeable. That is, Bar is a Foo subtype, so Foo objects can be replaced by Bar objects without having to change the properties of the program.
So my question is: Why does this code violate the "Strict Standards"?
Related: http://answall.com/questions/2413
– bfavaretto
Maybe because the guy in
Bar
is not set? Tried Object?– bfavaretto
@bfavaretto Gives the same error.
– Tivie
@bfavaretto I edited to clarify the problem better
– Tivie