php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65576 Constructor from trait conflicts with inherited constructor
Submitted: 2013-08-28 20:49 UTC Modified: 2014-12-12 15:57 UTC
Votes:10
Avg. Score:4.8 ± 0.6
Reproduced:10 of 10 (100.0%)
Same Version:0 (0.0%)
Same OS:5 (50.0%)
From: krewecherl at gmail dot com Assigned: jpauli (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.4.19 OS: *
Private report: No CVE-ID: None
 [2013-08-28 20:49 UTC] 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".

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-30 21:27 UTC] aharvey@php.net
-Assigned To: +Assigned To: gron
 [2013-08-30 21:27 UTC] aharvey@php.net
Stefan, since you fixed the original bug, do you have any thoughts on this one?
 [2013-08-30 22:02 UTC] gron@php.net
-Assigned To: gron +Assigned To:
 [2013-08-30 22:02 UTC] 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.
 [2014-12-08 14:29 UTC] dunglas at gmail dot com
We provided a patch for this bug: https://github.com/php/php-src/pull/946
 [2014-12-12 15:16 UTC] jpauli@php.net
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)
 [2014-12-12 15:16 UTC] jpauli@php.net
-Status: Open +Status: Closed
 [2014-12-12 15:16 UTC] jpauli@php.net
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)
 [2014-12-12 15:17 UTC] jpauli@php.net
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)
 [2014-12-12 15:18 UTC] jpauli@php.net
-Status: Closed +Status: Feedback
 [2014-12-12 15:18 UTC] jpauli@php.net
Please try using this snapshot:

  http://snaps.php.net/php5.5-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/

Merged against 5.5 and up
 [2014-12-12 15:57 UTC] jpauli@php.net
-Status: Feedback +Status: Closed -Assigned To: +Assigned To: jpauli
 [2014-12-12 15:57 UTC] jpauli@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 [2014-12-13 19:23 UTC] ab@php.net
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)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC