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
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: 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

Pull Requests

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: Sun Dec 22 06:01:30 2024 UTC