Bug #65576 [NEW]: Constructor from trait conflicts with inherited constructor
| From: | krewecherl at gmail dot com | Date: | Wed, 28 Aug 2013 20:49:06 +0000 |
| Subject: | Bug #65576 [NEW]: Constructor from trait conflicts with inherited constructor | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-181276@lists.php.net to get a copy of this message | ||
From: krewecherl at gmail dot com
Operating system: *
PHP version: 5.4.19
Package: Class/Object related
Bug Type: Bug
Bug description:Constructor from trait conflicts with inherited constructor
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 bug report at https://bugs.php.net/bug.php?id=65576&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=65576&r=trysnapshot54
Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=65576&r=trysnapshot53
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=65576&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=65576&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=65576&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=65576&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=65576&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=65576&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=65576&r=support
Expected behavior: https://bugs.php.net/fix.php?id=65576&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=65576&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=65576&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=65576&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65576&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=65576&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=65576&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=65576&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=65576&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=65576&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=65576&r=mysqlcfg