How can constructor definitions from traits collide?
In PHP 5.4.9, the following example triggers the fatal error "B has
colliding constructor definitions coming from traits".
trait T {
public function __construct () {
echo __CLASS__ . ": constructor called.\n";
}
}
class A {
use T;
}
class B extends A {
use T;
}
There's no problem when the trait contains a different method than the
constructor, and no problem when the constructor is actually copied into
the classes (without using traits, the "language-assisted copy & paste"
feature).
What's so special about the constructor here? Shouldn't PHP be able to
figure out that one of them overrides the other? I couldn't find anything
about this limitation in the manual.
This related question mentions a way to get around the problem (by using
aliases for trait methods), but not what's causing it in the first place.
No comments:
Post a Comment