Re: Idea: Implicit inheriting function argument & return type-hint
| From: | Christian Schneider | Date: | Sat, 09 Mar 2024 14:17:38 +0000 |
| Subject: | Re: Idea: Implicit inheriting function argument & return type-hint | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-122593@lists.php.net to get a copy of this message | ||
Am 09.03.2024 um 08:18 schrieb Marc <marc@mabe.berlin>:
> As the pseudo type
mixed exists since PHP 8.0, which is already
> out-of-date, I have the feeling it would be better to force people to explicitly write
> mixed to widen the type of an overwritten function and let "no
> type" inherit the argument and return type of the parent.
>
> Example:
>
> class A {
> public function typed(int $var): int { return $var; }
> public function untyped($var) { return $var; } // implicit mixed as no overwrite
> }
> class B extends A {
> public function typed($var) { return $var; } // implicit int as inherited from overwritten
> function
> }
This sounds like a bad idea to me as I cannot look at the function definition in class B and
determine what the type of $var is.
On top of that: If the type of $var in A is changed then it would automatically also change in B.
While this seems convenient it could also break B in subtle ways as the function suddenly gets
different types than expected.
Regards,
- Chris