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
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:
36 + 33 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 14:01:29 2024 UTC