php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79754 Covariance fails with mixed type
Submitted: 2020-06-28 13:06 UTC Modified: 2020-06-28 13:32 UTC
From: raincomplain at outlook dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 8.0.0alpha1 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: raincomplain at outlook dot com
New email:
PHP Version: OS:

 

 [2020-06-28 13:06 UTC] raincomplain at outlook dot com
Description:
------------
Since mixed type indicates the absence of a type then one would assume that omitting a return type in a child class's method that returns mixed type in a parent class is fine! but PHP refuses to compile

Test script:
---------------
class Foo {
    function fun(): mixed {
        return 33;
    }
}

class Bar extends Foo {
    function fun() {
        return 33;
    }
}

Expected result:
----------------
The expected is to compile with no error

Actual result:
--------------
Fatal error: Declaration of Bar::fun() must be compatible with Foo::fun(): mixed

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-28 13:32 UTC] derick@php.net
-Status: Open +Status: Not a bug
 [2020-06-28 13:32 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is expected behaviour. If a return type is missing, it is the implicit union of "mixed|void". As "mixed|void" is a wider type than just "mixed", allowing it would break LSP.

This is also explained in the RFC: https://wiki.php.net/rfc/mixed_type_v2#signature_checking_of_function_when_no_return_type_present
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Nov 28 00:03:13 2021 UTC