I think now the only way to do what you want is:
class MyHelloWorld extends Base { use SayWorld { SayWorld::__construct as private __swConstruct; } public function __construct($a, $b, $c = 0) { $this->__swConstruct($a, $b, $c); } }
Edit 2:
My advice, based on over a year of working with traits in PHP, is this: avoid writing constructors in traits at all , or if you should - at least make them without parameters. Their presence in features contradicts the idea of constructors as a whole, namely: constructors must be specific to the class to which they belong. Other, developed, high-level languages do not even support implicit constructor inheritance. This is because constructors have a much stronger relationship to the class than other methods. In fact, they have such a strong relationship that even LSP does not apply to them. Traits in the Scala language (very mature and SOLID is the friendly successor to Java) cannot have a constructor with parameters .
Change 1:
There was a bug in PHP 5.4.11 that actually allowed the alias to use the superclass method. But the PHP developers considered this a no-no, so we still stick to this cumbersome solution that I presented above. But this mistake raised a discussion about what can be done with it, and I hope that it will be aimed at future releases.
Meanwhile, I came across the same problem over and over again. My irritation increased exponentially with the number of parameters and docblock lines that had to be repeated many times in order to use this trait. So I came up with the following scheme to adhere to the DRY rule as far as I could:
Instead of repeating the whole set of such parameters:
trait SayWorld { public function __construct($a, $b) { echo (int)$c * ($a+$b); } } class MyHelloWorld extends Base { use SayWorld { SayWorld::__construct as private __swConstruct; } public function __construct($a, $b, $c = 0) { $this->__swConstruct($a, $b); } }
I am writing a class that looks like a tuple (the concept is familiar to C # and Python ) and use it instead of an infinite list of parameters:
class SayWorldConstructTuple { public $a; public $b; public function __construct($a, $b) { $this->a = $a; $this->b = $b; } } class MyHelloWorld extends Base { use SayWorld { SayWorld::__construct as private __swConstruct; } public function __construct(SayWorldConstructTuple $Tuple, $c = 0) { $this->__swConstruct($Tuple->a, $Tuple->b); $this->c = $c; } }
Note: this template, of course, is more useful with a large number of tuple constructor options and more classes using a tuple.
It can be further automated using the dynamic nature of PHP.