Req #80921 [Opn->Wfx]: allow parameter type hints for poorly typed interface methods
| From: | requinix@php.net | Date: | Tue, 30 Mar 2021 20:51:50 +0000 |
| Subject: | Req #80921 [Opn->Wfx]: allow parameter type hints for poorly typed interface methods | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-233077@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80921&edit=1
ID: 80921
Updated by: requinix@php.net
Reported by: smiley at chillerlan dot net
Summary: allow parameter type hints for poorly typed
interface methods
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Class/Object related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
No parameter type means "mixed", as in any type is accepted. Allowing a child class like
StronglyTypedClass to be more restrictive about its *parameter types* violates LSP and the principle
of contravariance. *Return types*, on the other hand, can be made more restrictive.
https://en.wikipedia.org/wiki/Liskov_substitution_principle
https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)
Previous Comments:
------------------------------------------------------------------------
[2021-03-30 20:31:08] smiley at chillerlan dot net
Description:
------------
Hello! I'd like to be able to add parameter type hints for poorly typed interface methods for
type security and consistency.
We're currently allowed to add return types in case they're not declared in the interface
or omit existing type hints in the interface, but for some reason the aforementioned probably most
important feature is missing.
A userland example would be the dilemma of a PSR-7 update: https://github.com/php-fig/http-message/pull/83
I understand that this might require an RFC, but i don't possess neither the technical insight,
nor the mental capacity to start such a process.
Don't mind me if plans/feature request for such an update already exist. :)
Test script:
---------------
<?php
// a poorly typed interface
interface PoorlyTypedInterface{
public function methodWithoutTypeHints($string, $int);
}
// currently we're only allowed to add a return type
class PoorlyTypedClass implements PoorlyTypedInterface{
public function methodWithoutTypeHints($string, $int):array{
return [$string, $int];
}
}
// proposal: allow adding parameter type hints
class StronglyTypedClass implements PoorlyTypedInterface{
public function methodWithoutTypeHints(string $string, int $int):array{
return [$string, $int];
}
}
Actual result:
--------------
Fatal error: Declaration of StronglyTypedClass::methodWithoutTypeHints(string $string, int $int):
array must be compatible with PoorlyTypedInterface::methodWithoutTypeHints($string, $int)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80921&edit=1