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