|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-01-05 18:27 UTC] bitluni at bitluni dot net
Description:
------------
When extracting traits I would like to extract some general functions into sub traits and 'use' them in the traits where needed.
But when I compose a class out of multiple traits with sub traits I have to do obsolete statements regardless if the colliding methods originate from same sub trait.
Just add a check if a method collides with itself
Test script:
---------------
trait A
{
public function a(){}
}
trait B
{
use A;
}
trait C
{
use A;
}
class D
{
use B, C;
}
Actual result:
--------------
Fatal error: Trait method a has not been applied, because there are collisions with other trait methods on D in
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 22:00:01 2025 UTC |
In my humble opinion, a solution/improvement would be that precedence order should not only be appliccable to classes, but also to traits. To expand on the given example at the start of this topic, when: trait A { public function a(){} public function b(){} public function c(){} public function z(){} } trait B { use A; } trait C { use A; } class D { use B, C; } The expected result here would be that methods in trait C override methods in trait B, unless explicitly specified using the insteadof operator. Shortened example using the insteadof operator: class D { use B; use C { B::a insteadof C; } } Here, the expected results would be that methods in trait C override methods in trait B, except for method B::a() which is explicitly defined to be used instead of C::a() If one would change which methods would override other methods, one would simply change the precedence other, as with the example: class D { use C; use B { C::a insteadof B; } } Here, the expected results would be that methods in trait B override methods in trait C, except for method C::a() which is explicitly defined to be used instead of B::a() Am I making sense?I would like to add another test case to this issue. It should handle same methods not only from sub-traits but also from current class. Thanks! <?php trait helper { public function helper() {} } trait util_useful { use helper; public function do_something_useful() {$this->helper(); return 'useful';} } trait util_useless { use helper; public function do_something_useless() {$this->helper(); return 'useless';} } class utils { use helper; use util_useful; use util_useless; public function use_helper() { $this->helper();} } $ob = new utils(); echo $ob->do_something_useful(), "\n"; echo $ob->do_something_useless(), "\n"; echo $ob->use_helper(), "\n"; ?>