Closed otsch closed 11 months ago
Something to be aware of is that it could be a breaking change, particularly in packages where someone might override one of your classes.
For example:
class Foo
{
- public function test(string $arg = null)
+ public function test(?string $arg = null)
{
//
}
}
class Bar extends Foo
{
public function test(string $arg = null)
{
// Fatal Error:
// Declaration of Bar::test(string $arg) must be compatible with Foo::test(?string $arg)
}
}
Tried it and I don't get a fatal:
I guess PHP internally knows that there is no difference if the type is explicitly or implicitly nullable. Or do you really get the fatal error @jasonvarga?
Yeah I really get the error. Here you can see it in a browser and cli. I don't think there's anything special about my setup that would show an error where others wouldn't see one. I could be wrong though!
@jasonvarga in your image you are missing the '= null'.
@jasonvarga
In this screenshot the $name
argument has no default value. That makes the difference.
?string $name
and string $name
definitely is not the same.
But ?string $name = null
and string $name = null
, basically is the same, as the null
default value makes the string
type implicitly nullable.
Ah crap. You're absolutely right. Sorry for the bother! ☺️ 🤐 🤦♂️
Because support for implicit support for implicit nullability exists only for backwards compatibility reasons and will be removed in the future. See https://github.com/laravel/pint/issues/235.