Bug #65576 [Opn->Csd]: Constructor from trait conflicts with inherited constructor
| From: | jpauli@php.net | Date: | Fri, 12 Dec 2014 15:16:35 +0000 |
| Subject: | Bug #65576 [Opn->Csd]: Constructor from trait conflicts with inherited constructor | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-189036@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
Updated by: jpauli@php.net
Reported by: krewecherl at gmail dot com
Summary: Constructor from trait conflicts with inherited
constructor
-Status: Open
+Status: Closed
Type: Bug
Package: Class/Object related
Operating System: *
PHP Version: 5.4.19
Block user comment: N
Private report: N
New Comment:
Automatic comment on behalf of dunglas@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=ee226b961c2871dd2f454fa707fc23e4af5cf263
Log: Fixed Bug #65576 (Constructor from trait conflicts with inherited constructor)
Previous Comments:
------------------------------------------------------------------------
[2014-12-08 14:29:54] dunglas at gmail dot com
We provided a patch for this bug: https://github.com/php/php-src/pull/946
------------------------------------------------------------------------
[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