Bug #62069 [Asn]: binding wrong traits if they have same name methods
| From: | gron@php.net | Date: | Sun, 20 May 2012 07:19:52 +0000 |
| Subject: | Bug #62069 [Asn]: binding wrong traits if they have same name methods | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-170603@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62069&edit=1
ID: 62069
Updated by: gron@php.net
Reported by: laruence@php.net
Summary: binding wrong traits if they have same name methods
Status: Assigned
Type: Bug
Package: Scripting Engine problem
PHP Version: 5.4.3
Assigned To: dmitry
Block user comment: N
Private report: N
New Comment:
@laruence it is intended behavior to have no fatal error in this case.
The method of the class has precedence and solves the conflict explicitly.
If the class does not define a method with that name, the conflict remains
unsolved, and you will get an error.
Previous Comments:
------------------------------------------------------------------------
[2012-05-20 04:53:35] laruence@php.net
@gron, the second example is try to description another bug of the first example,
that is , if the class have a own defined same name as trait, no fatal error
occurred, :)
------------------------------------------------------------------------
[2012-05-19 13:55:05] gron@php.net
Hi Laruence:
The first example is a bug, indeed.
Why is the second example a bug?
To me it looks like the perfectly intended traits semantics, no?
------------------------------------------------------------------------
[2012-05-19 06:44:30] laruence@php.net
and if the class have no own func method defination, the result will be:
<?php
trait T1 {
public function func() {
echo "From T1\n";
}
}
trait T2 {
public function func() {
echo "From T2\n";
}
}
class Bar {
use T1 {
func as f1;
}
use T2 {
func as f2;
}
}
PHP Fatal error: Trait method func has not been applied, because there are
collisions with other trait methods on Bar in /tmp/1.php on line 21
this should also be a bug. thanks :)
------------------------------------------------------------------------
[2012-05-19 06:40:54] laruence@php.net
Hi, dmitry, could you please look at this? thanks
the problem occurrs in zend_traits_merge_functions. in that function, it will
unique the trait methods name.
the fix will be a big change, since we should re-implemention the whole merge
functions mechanism, maybe add the tarit name as a suffix to method name.
thanks
------------------------------------------------------------------------
[2012-05-19 06:30:40] laruence@php.net
Description:
------------
if a function binding more than two traits and they have same method name, the
result is wrong.
Test script:
---------------
<?php
trait T1 {
public function func() {
echo "From T1\n";
}
}
trait T2 {
public function func() {
echo "From T2\n";
}
}
class Bar {
public function func() {
echo "From Bar\n";
}
use T1 {
func as f1;
}
use T2 {
func as f2;
}
}
$b = new Bar();
$b->f2();
Expected result:
----------------
From T2
Actual result:
--------------
From T1
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62069&edit=1