|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 18:00:02 2025 UTC |
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; } }