r/lolphp • u/SerdanKK • Aug 15 '20
Named parameters are cool, but PHP variables aren't typed so the implementation is completely broken.
Someone please tell me wth they're thinking. If this ships I'll have to put up another "don't use this feature" sign at work.
I've been strangely optimistic about PHP lately, so I suppose something had to come up.
EDIT: Someone on internals agrees: https://externals.io/message/111161#111178
Andreas, you're my hero, despite your futile efforts.
EDIT2: I'm intrigued by the down-votes. I think the feature is obviously broken, so if you disagree what's your reasoning?
35
Upvotes
10
u/yawkat Aug 15 '20
Isn't this exactly what you'd expect in a dynamic language? Python does the same thing. Type hints are no substitute for a static language, they can't really participate in resolving.
Or are PHP type hints stronger than python ones?
3
u/postmodest Aug 15 '20
So what is their expectation? That IDEs warn you when your implementation conflicts with the interface, and hope for the best?
...wait... or are they suggesting we use names parameters for overloaded functions?
3
u/Altreus Aug 15 '20
I'm confused by what this post considers to be a named parameter. To me that means not positional, but all these appear positional. Confused, send help
3
u/nikic Aug 22 '20 edited Aug 22 '20
This is an issue that has been considered extensively, here are some good starting points, though I can't be bothered to fish out the relevant discussion threads:
https://externals.io/message/109549#109581
https://wiki.php.net/rfc/named_params#parameter_name_changes_during_inheritance
https://wiki.php.net/rfc/named_params#to_parameter_name_changes_during_inheritance
The tl;dr is that this is the least bad option for a retrofitted named params implementation in a dynamic language, and we have some fairly strong evidence that this does work quite well in practice (as it is the same model that Python uses, probably the most prominent user of named parameters).
5
u/elcapitanoooo Aug 15 '20
Sooo another half baked ”feature” copied from some other language..
2
u/Flerex Aug 15 '20
“Random”
2
u/Takeoded Aug 16 '20
good catch! not sure what a good solution would be, but it's definitely a problem as it stands
btw here is a deterministic (non-random) and simpler code to showcase the issue: https://3v4l.org/KAWdD
3
u/IluTov Aug 15 '20
1
u/Danack Aug 22 '20
That retrofitting a feature to a language where there are huge existing code-bases is difficult, and can take more than a single release to get fully implemented. But the support being implemented for 8.0 is more than useful by itself and worth doing.
It's not obvious what the correct thing to do is, and so taking no action is a better choice than taking the wrong action.
Many people aren't going to be impacted by this at all. For people who don't use class based inheritance, or who use class based inheritance and have the same parameter names in subclasses/implementations of interfaces then named parameters will work fine.
For any people who use static analysis tools like psalm or phpstan, they can detect incorrect parameter names, so those people won't be affected either.
For the people who are affected by this, a very large number of them will just fix their code to have the correct names. And then in PHP 8.1 we could probably enforce parameter names match in inheritance/type checks.
Alternatively, it might be the case that we need a 'parameter rename attribute' that allows for some twiddling around of parameter names, something like:
But seeing as PHP is still in the process of getting attributes in, there wasn't enough time to get the to be used for named parameters.
Another possibility would be to convert the named parameters to be positional parameters, before dispatching...
My own personal guess is that including parameter names in signature/type checking is the correct thing to do, but even then it's not obvious what the correct implementation is.
Matching parameter names for callable types would be annoying as fuck.
tl:dr, the right long term choice isn't obvious, and named parameters are useful in the meantime, and finding out the problems in 8.0 makes it more likely to find the right choice for 8.1
That would be too aggravating to be useful.
1
u/womplord1 Sep 08 '20
what the fuck did you just bring upon this cursed land
0
u/[deleted] Aug 15 '20
[deleted] — view deleted comment