On 8/6/25 1:41 PM, Rob Landers wrote:
Take for example:
class Fruit {
public string $kind { get => "fruit" }
}
class Vegetable extends Fruit {
public string $kind { get => "vegetable" }
}
function foo(Fruit $fruit) {}
foo(new Vegetable); // hmmm
This is a "soft" violation that only makes sense to us humans, but PHP
allows it. It requires us humans to realize we are performing an LSP
violation and refactor the code so that we don't pass a Carrot to
someone expecting a Mango.
Rob, that's not how types work. The only thing guaranteed by this
definition of $fruit->kind is that it has a getter that returns a
string. "vegetable" is a string. This isn't a violation at all.
Now if you were able to type specific values in PHP a la psalm/phpstan
and make something like this:
class Fruit {
public "apple"|"pear" $kind { get => "fruit" }
}
Well then yeah Vegetable would be a violation, but this would be a
different (and more specific) type than just string. (And this is why
you can't extend enums, it would widen a contract)
This isn't a "soft" violation that "PHP allows", this is a well defined
property of every programming language I know of.