[PHP-BUG] Req #75823 [NEW]: Proposal: Make a covariant types

[email protected] ("hack3p at gmail dot com") Tue, 16 Jan 2018 15:05:27 GMT
Newsgroups php.standards
Message-ID <[email protected]>
From:             hack3p at gmail dot com
Operating system: irrelevant
PHP version:      Irrelevant
Package:          PHP Language Specification
Bug Type:         Feature/Change Request
Bug description:Proposal: Make a covariant types

Description:
------------
The current invariative type interferes with writing idiomatic
inheritance, which is implemented in many other languages. 

Now us have to completely remove the type of the return value and type
and remove functions type hintings arguments in the parent and childs
class, which helps to solve this. But in this case, in business logic
now you need to manually implement type checking, since a secure
application should work with the correct type. 

At the advent of PHP 7 with the possibility of strict typing, I hoped
that the types will finally become variative. However, this did not
happen. 

RFC by return types (https://wiki.php.net/rfc/return_types) in section
"Variance and Signature Validation" has next: 
> This RFC originally proposed covariant return types but was changed to
invariant because of a few issues. It is possible to add covariant
return types at some point in the future. 

I think it's time to move towards idiomatic inheritance, since it will
be correct if PHP to check the types, not the programmer.

Test script:
---------------
Test script:
https://gist.github.com/hack3p/78eeca8f50d3297bf25768a9c72cafeb

Expected result:
----------------
Previous test script should be work

Actual result:
--------------
example 1: return type
PHP Fatal error:  Declaration of B::foo(): B must be compatible with
A::foo(): A in ...

example 2: arguments type hinting
PHP Warning:  Declaration of B::foo(B $bar) should be compatible with
A::foo(A $bar) in ...

-- 
Edit bug report at https://bugs.php.net/bug.php?id=75823&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=75823&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=75823&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=75823&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=75823&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=75823&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=75823&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=75823&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=75823&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=75823&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=75823&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=75823&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=75823&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=75823&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75823&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=75823&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=75823&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=75823&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75823&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=75823&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=75823&r=mysqlcfg