php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #73010 Can't override interfaces' method return type with its subtypes
Submitted: 2016-09-04 09:53 UTC Modified: 2020-03-05 17:58 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: alan dot bem at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Class/Object related
PHP Version: 7.1.0RC1 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: alan dot bem at gmail dot com
New email:
PHP Version: OS:

 

 [2016-09-04 09:53 UTC] alan dot bem at gmail dot com
Description:
------------
Affected PHP versions: 7.0.0 - 7.1.0beta3

When implementing interfaces, it is impossible to override return types declarations with their (return types) subtypes/implementations although it is perfectly fine with LSP.

Test script:
---------------
https://3v4l.org/MgU9b

Now I get error: "Fatal error: Declaration of CustomerFactory::create(): Customer must be compatible with AggregateRootFactory::create(): AggregateRoot in /file/path on line 15"

I expect: Customer type is implementation of AggregateRoot and it should work perfectly fine.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-09-04 10:10 UTC] requinix@php.net
While allowed by LSP, return types are deliberately invariant due to implementation issues. That may change in the future.
https://wiki.php.net/rfc/return_types#variance_and_signature_validation

This is a legitimate request so I'm not marking this as NAB/Wontfix/Suspended, but currently there are no plans to add support for covariance.
 [2020-03-05 17:58 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-03-05 17:58 UTC] nikic@php.net
This is supported as of PHP 7.4.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 07:01:30 2024 UTC