Idea: Warning about named parameters with a missing constructor?
| From: | tyson andre | Date: | Tue, 20 Oct 2020 23:00:43 +0000 |
| Subject: | Idea: Warning about named parameters with a missing constructor? | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-112083@lists.php.net to get a copy of this message | ||
Hi internals,
Currently, if there is no constructor, php handles it just like
__construct(...$args)
{},
both for positional and named parameters. Is there any interest in changing that to be a deprecation
warning if one or more parameters are passed?
(or if 1 or more named parameters are passed)
The default behavior of missing constructors would result in somewhat surprising behavior
for new $className(paramName: 123) if this was used unexpectedly
(silently ignoring it)
(Desired behavior would be to deprecate/warn/throw)
```
php > class A {}
php > var_export(new A(paramName: 123));
A::__set_state(array(
))
php > var_export(new stdClass(paramName: 123));
(object) array(
)
php > var_export(new stdClass(123));
(object) array(
)
```
(I don't think https://wiki.php.net/rfc/named_params mentioned
this
(the current behavior follows from handling of extra positional parameters),
and this mistake is probably unlikely in practice,
but if it were to occur it would very likely be a bug)
PHP already throws ArgumentCountError for passing too many parameters for internal methods
so the default method not throwing is a surprise. I haven't yet looked into how much code would
break if this were changed.
Aside: If a static analyzer such as Phan, Psalm, or PHPStan are set up, those bugs could be detected
easily,
so the impact seems low, but I wonder if this type of bug could cause confusion (or feel unexpected)
for those who are more familiar with other languages than they are with php..
Thanks,
- Tyson