php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69911 Interface apply to trait
Submitted: 2015-06-23 11:14 UTC Modified: 2020-03-06 10:25 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: igor at glagola dot ru Assigned:
Status: Not a bug Package: Compile Failure
PHP Version: 5.6.10 OS: Linux
Private report: No CVE-ID: None
 [2015-06-23 11:14 UTC] igor at glagola dot ru
Description:
------------
Hi, I've got strange name collision, interface applies to trait's method before trait's method rename statement.

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

interface SomeInterface {
    public function theAction();
}

trait TheTrait {
    public function theAction($param) {
    }
}

class CC {
    public function theAction() {

    }
}

class BB extends CC implements SomeInterface {
    use TheTrait {
        TheTrait::theAction as otherAction;
    }
}


Expected result:
----------------
I expect no errors

Actual result:
--------------
PHP Fatal error:  Declaration of TheTrait::theAction() must be compatible with SomeInterface::theAction() in /tmp/a.php on line 22
PHP Stack trace:
PHP   1. {main}() /tmp/a.php:0

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-06 10:25 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2020-03-06 10:25 UTC] nikic@php.net
The "T::m as m2" syntax does not rename the method, it adds an alias. As such, the trait method with the original name is still imported and does indeed inflict with the interface.

I don't think there is any way to prevent the trait method from being added (all conflict resolution is about conflicts between traits, while here you want to say that the parent method should be used). I think the way around it is to write this:

class BB extends CC implements SomeInterface {
    use TheTrait {
        TheTrait::theAction as otherAction;
    }
    public function theAction() {
        return parent::theAction();
    }
}
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Jun 07 03:01:24 2020 UTC