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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
12 + 24 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 12:01:27 2024 UTC