Bug #62069 [Asn]: binding wrong traits if they have same name methods
| From: | dmitry@php.net | Date: | Tue, 22 May 2012 06:25:47 +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-170660@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: dmitry@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:
I would say it's not an implementation bug, but design mistake that allows ambiguous syntax.
The problem that class may include several "use" statements, each statement may refer
several traits and each such statement may be followed by a block with alias declarations. However,
all these blocks have equal rights and it doesn't mean which traits were used in
"use" statement before.
So the second example is treated as:
use T1, T2 {
func as f1;
func as f2;
}
To refer to the proper traits methods need to be qualified
use T1, T2 {
T1::func as f1;
T2::func as f2;
}
I'll try to take a look, if it's possible to fix it, but I don't think it's
possible to do it in 5.4 (without binary compatibility break).
Previous Comments:
------------------------------------------------------------------------
[2012-05-22 02:24:52] laruence@php.net
@dmitry, as a common guess, the second should have a high priority, I think it
should be a bug ... :)
------------------------------------------------------------------------
[2012-05-21 22:13:05] gron@php.net
Dmitry, there is nothing wrong with Laruence original example.
Except, that it indeed points out a bug.
------------------------------------------------------------------------
[2012-05-21 12:15:06] dmitry@php.net
I think this is not a bug (at least according to documentation http://php.net/manual/en/language.oop5.traits.php).
The alias defined using "as" statement adds an *additional* name but doesn't resolve
the conflict.
The proper test would be:
<?php
trait T1 {
public function func() {
echo "From T1\n";
}
}
trait T2 {
public function func() {
echo "From T2\n";
}
}
class Bar {
use T1, T2 {
T1::func insteadof T2;
T1::func as f1;
T2::func as f2;
}
}
$x = new Bar();
$x->f1();
$x->f2();
$x->func();
Expected result:
----------------
From T1
From T2
From T1
It's passed fine, however the whole traits concept is messed and not intuitive.
------------------------------------------------------------------------
[2012-05-20 08:41:16] laruence@php.net
@gron, oh, I see, thanks.
------------------------------------------------------------------------
[2012-05-20 07:19:52] gron@php.net
@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.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=62069
--
Edit this bug report at https://bugs.php.net/bug.php?id=62069&edit=1