|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2021-07-16 15:07 UTC] nicolasgrekas@php.net
Description:
------------
This code yields "Default value for property of type X&Y may not be null. Use the nullable type ?X&Y to allow null default value":
class Test {
public X&Y $y = null;
}
Fine, let's add the suggested "?". But then I get "Parse error: syntax error, unexpected token "&", expecting variable".
About returning null|intersection, should this be possible?
class Test {
function foo(): ?X&Y
{
return null;
}
}
Right now it yields a syntax error.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 04:00:02 2025 UTC |
This should be brought to php-internals IMHO. The fact that "function (X&Y $foo = null) {};" works is going to be super useful. But the fact that there is no type to express the type of the argument is WTF. It would be consistent to allow ?X&Y everywhere.> This should be brought to php-internals IMHO. > The fact that "function (X&Y $foo = null) {};" works is going to be super useful. I don't deny that, but the limitation that you cannot combine union types with intersection was explicitly mentioned in the RFC. And a nullable type is a union type of T|null. > But the fact that there is no type to express the type of the argument is WTF. I agree that's a WTF, but that's an issue because of PHP's implicit nullable type, which should die IMHO. > It would be consistent to allow ?X&Y everywhere. ?X&Y for means (?X)&Y which is bogus/redundant, if we allow a nullable syntax it should be ?(X&Y) but this syntax was dropped from the union type RFC because many people opposed it (https://wiki.php.net/rfc/union_types_v2#supported_types see Nullable Union types section) So feel free to bring this up on php-internals, but I am *vehemently* against adding support for nullable intersection types, and I consider the implicit nullable type behaviour a bug. Especially that I have *no idea* if it follows LSP checks correctly as that was the *main* reason to not support mixing unions and intersections in the first place.