php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80391 Iterable not covariant to mixed
Submitted: 2020-11-20 19:34 UTC Modified: 2020-11-24 09:02 UTC
Votes:5
Avg. Score:4.8 ± 0.4
Reproduced:5 of 5 (100.0%)
Same Version:5 (100.0%)
Same OS:3 (60.0%)
From: peldax at gmail dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 8.0.0RC5 OS: All
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:
41 - 14 = ?
Subscribe to this entry?

 
 [2020-11-20 19:34 UTC] peldax at gmail dot com
Description:
------------
Hello,

When I declare a function in a class/interface with "mixed" return type, and than extend the class and apply covariant return type "iterable", I get compatibility error saying that functions are not compatible.

Code snippet bellow gives me:

Fatal error: Declaration of Child::getValue(): iterable must be compatible with SomeInterface::getValue(): mixed

I am not sure if "iterable" has any special treatment (it works correctly for other types, such as int, self, Child, ...) or is it just forgotten pseudo type.

Thank you.

Václav Pelíšek @peldax

Test script:
---------------
<?php

interface SomeInterface {
    public function getValue() : mixed;
}

class Child implements SomeInterface
{
    public function getValue() : iterable
    {
        return [];
    }
}

Expected result:
----------------
Script compiles successfully.

Actual result:
--------------
Fatal error: Declaration of Child::getValue(): iterable must be compatible with SomeInterface::getValue(): mixed

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-11-20 19:56 UTC] peldax at gmail dot com
Strange addition:

Contravariace works correctly, following script compiles succesfully:

<?php

interface SomeInterface {
    public function getValue(iterable $arg) : void;
}


class Child implements SomeInterface
{
    public function getValue(mixed $arg) : void
    {
        return;
    }
}
 [2020-11-24 09:02 UTC] nikic@php.net
-Assigned To: +Assigned To: nikic
 [2020-11-24 09:10 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=912cb8b8b52f958ac6eb482466c01e99fc0c0e35
Log: Fixed bug #80391
 [2020-11-24 09:10 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 18:01:28 2024 UTC