php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80785 Complex inheritance depends on interface order
Submitted: 2021-02-22 22:39 UTC Modified: -
Votes:3
Avg. Score:3.7 ± 1.2
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: peldax at gmail dot com Assigned:
Status: Open Package: *General Issues
PHP Version: 8.0.2 OS: All
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2021-02-22 22:39 UTC] peldax at gmail dot com
Description:
------------
Hello,

I have come across some special behaviour in inheritance. I am not sure how to clearly describe the bug, please come to see the example below.

Code below throws following error:

Fatal error: Declaration of ParentType::accept(ParentVisitor $visitor): mixed must be compatible with ChildType::accept(ChildVisitor $visitor): mixed in /in/sYmmm on line 9


BUT the code in the example works correctly when I switch the order of interfaces in extends for the "Final" interface.

interface Final extends ChildType, OtherInterface {}

Compiles successfully.


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

interface ChildVisitor { /* some visiting methods */ }

interface ParentVisitor extends ChildVisitor { /* some extra visiting methods */ }

interface ParentType
{
    public function accept(ParentVisitor $visitor) : mixed;
}

interface ChildType extends ParentType
{
    public function accept(ChildVisitor $visitor) : mixed;
}

interface OtherInterface extends ParentType {}

interface Final extends OtherInterface, ChildType {}

Expected result:
----------------
Compile successfully.

Actual result:
--------------
Fatal error: Declaration of ParentType::accept(ParentVisitor $visitor): mixed must be compatible with ChildType::accept(ChildVisitor $visitor): mixed in /in/sYmmm on line 9

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-22 22:45 UTC] peldax at gmail dot com
Ofc the name of the Final interface cannot be "Final",

Here is the link for the live preview: https://3v4l.org/7u3Pn
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Apr 20 02:01:24 2021 UTC