Open
Description
Description
The following code:
<?php
class Foo {}
class Bar {}
function test(Foo&Bar $arg): void {}
I use intersection type Foo&Bar
for function argument.
Actually, there is no type that would be comparable to Foo&Bar
. Therefore, Foo&Bar
is equivalent to the type never
.
If i use never
in argument type, PHP Fatal Error will be thrown. But if i use Foo&Bar
, no error will occur.
Resulted in this output:
Nothing
But I expected this output instead:
PHP Fatal error: Foo&Bar cannot be used as a parameter type
PHP Version
PHP 8.3.11
Operating System
Ubuntu 20.04
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
iluuu1994 commentedon Sep 1, 2024
Hi @Kenny1911. This is something static analysis can help you with. IMO, this doesn't need solving in the PHP engine, as it will just add more runtime burden. @Girgias thoughts?
https://phpstan.org/r/a1a9efce-9725-40d1-818a-595a25e65c12
vudaltsov commentedon Sep 1, 2024
@iluuu1994 , however PHP does check
int&float
, for example. Is it difficult to check this as well?iluuu1994 commentedon Sep 1, 2024
That's not quite the same:
The engine makes assertions about members of intersection types being class types.
I'm not sure about hard, but at least for
Foo&Bar
, it would necessarily have to happen at runtime, when both types are available.Girgias commentedon Sep 1, 2024
Yes, as currently class-types are not loaded at compile time except where necessary (for variance checks).
Changing this behaviour would at least require an RFC to allow the engine to autoload classes where previously it was not required.
Checking for
int&float
is easy as they are built-in types.iluuu1994 commentedon Sep 1, 2024
Right. Given this would either require to:
I don't think this should be solved.
cmb69 commentedon Sep 1, 2024
I agree to WONTFIX, but we should probably improve the documentation on intersection types.
[-]Argument type is intersection, comprised of the classes without common parent[/-][+]Impossible intersection types are not checked at compile time[/+]Girgias commentedon Sep 9, 2024
Just to note, that this is described in the type declaration page of the documentation.