Bug #65576 [Com]: Constructor from trait conflicts with inherited constructor
| From: | dunglas at gmail dot com | Date: | Mon, 08 Dec 2014 14:29:54 +0000 |
| Subject: | Bug #65576 [Com]: Constructor from trait conflicts with inherited constructor | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-188960@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=65576&edit=1
ID: 65576
Comment by: dunglas at gmail dot com
Reported by: krewecherl at gmail dot com
Summary: Constructor from trait conflicts with inherited
constructor
Status: Open
Type: Bug
Package: Class/Object related
Operating System: *
PHP Version: 5.4.19
Block user comment: N
Private report: N
New Comment:
We provided a patch for this bug: https://github.com/php/php-src/pull/946
Previous Comments:
------------------------------------------------------------------------
[2013-08-30 22:02:31] gron@php.net
Without having looked into the implementation or the tests, the given script looks sensible. I
don't remember any
reason why it should not work.
I can only guess that the check does not recognize that the constructor originates in the base class
and therefore
thinks it is a conflict.
------------------------------------------------------------------------
[2013-08-30 21:27:46] aharvey@php.net
Stefan, since you fixed the original bug, do you have any thoughts on this one?
------------------------------------------------------------------------
[2013-08-28 20:49:06] krewecherl at gmail dot com
Description:
------------
Given:
- a trait T containing a __construct method
- a base class A with a __construct method (directly, inherited or via traits)
- a class B exending A and using trait T
Compilation fails with the fatal error "B has colliding constructor definitions
coming from traits".
Any other method (including magic methods like __set) works fine in this
scenario, with the constructor being the only exception. It should be possible
to use a constructor from a trait in a derived class, even if the base class
already has a constructor. The collision can be resolved by prioritizing the
constructor from the trait over the inherited constructor.
This problem is the result of a bugfix in zend_compile.c related to issue #55554
(Trait methods overriding legacy constructors):
https://github.com/php/php-src/commit/e14354af21c9188582ef454696163cf68c7677ce
I understand the problem, but think that the given combination should still be
made to work. If there are insurmountable technical difficulties, the special
exception for constructors needs to be documented in the manual.
Test script:
---------------
trait T {
public function __construct () {}
}
class A {
public function __construct () {}
}
class B extends A {
use T;
}
Expected result:
----------------
Class B uses the constructor included via trait T, overriding base class A's
original constructor.
Actual result:
--------------
Fatal error "B has colliding constructor definitions coming from traits".
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65576&edit=1