81

In PHP 5, I can to overload constructors (and any others methods). But if I get some code like this:

class Base {

    public function __construct($a, $b) {
        echo $a+$b;
    }


    public function sayHello() {
        echo 'Hello ';
    }
}


trait SayWorld {

    public function __construct($a, $b, $c = 0) {
        echo (int)$c * ($a+$b);
    }

    public function sayHello($a = null) {
        parent::sayHello();
        echo 'World!'.$a;
    }
}

class MyHelloWorld extends Base {
    use SayWorld;
}

$o = new MyHelloWorld(2, 3);
$o->sayHello(1);

I have an error:

Fatal error: MyHelloWorld has colliding constructor definitions coming from traits

How can I to fix it? You can test my code here.

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Guy Fawkes
  • 2,313
  • 2
  • 22
  • 39
  • 1
    Just a warning. Trait aliases will cause PHP to crash as of 5.4.7, particularly with autoloaders. A fix has been added to the repo, so hopefully it will show up in the next version. – Matthew Sep 25 '12 at 13:36

3 Answers3

155

I think for 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 dealing with traits in PHP, is: avoid writing constructors in traits at all, or if you must - at least make them parameterless. Having them in traits goes against the idea of constructors in general, which is: constructors should be specific to a class to which they belong. Other, evolved high-level languages don't even support implicit constructor inheritance. This is because constructors have far more stronger relation to the class then other methods. In fact they have so strong relation, that even the LSP does not apply to them. The traits in Scala language (a very mature and SOLID-friendly successor of Java), can't have a constructor with parameters.

Edit 1:

There was a bug in PHP 5.4.11, which actually allowed to alias a superclass method. But this was considered a no-no by the PHP developers, so we are still stuck with that cumbersome solution which I presented above. But that bug raised a discussion about what can be done with this, and I'm hoping it will be targeted in future releases.

Meanwhile I came across the same problem over and over again. My irritation raised exponentially with the number of parameters and lines of docblock which had to be repeated a lot of times in order to use the trait. So I came up with the following pattern in order to stick to the DRY rule as much as I could:

Instead of repeating entire set of parameters like this:

trait SayWorld {

    /**
     * This is a valid docblock.
     *
     * @param int $a Doc comment.
     * @param int $b Doc comment.
     */
    public function __construct($a, $b) {
        echo (int)$c * ($a+$b);
    }
}

class MyHelloWorld extends Base {

    use SayWorld {
        SayWorld::__construct as private __swConstruct;
    }

    /**
     * Repeated and unnecessary docblock.
     *
     * @param int $a Doc comment.
     * @param int $b Doc comment.
     * @param int $c Doc comment.
     */
    public function __construct($a, $b, $c = 0)
    {
        $this->__swConstruct($a, $b);
    }
}

I write a class much like a tuple (concept familiar to C# and Python users), and use it instead of an endless 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;
    }

    /**
     * New and valid docblock.
     *
     * @param SayWorldConstructTuple $Tuple
     * @param int $c Additional parameter.
     */
    public function __construct(SayWorldConstructTuple $Tuple, $c = 0)
    {
        $this->__swConstruct($Tuple->a, $Tuple->b);
        $this->c = $c;
    }
}

Note: this pattern is of course more useful with a larger amount of tuple's constructor parameters, and more classes using the tuple.

It can be automated further with the use of PHP's dynamic nature.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Maciej Sz
  • 11,151
  • 7
  • 40
  • 56
7

Try:

use SayWorld {
  Base::__construct insteadof SayWorld;
}

Ref: PHP Docs

Martin
  • 6,632
  • 4
  • 25
  • 28
  • I tried it. Did **you** tried to run your own code? It returns MyHelloWorld has colliding constructor definitions coming from traits fatal error anyway. – Guy Fawkes Sep 18 '12 at 13:46
  • 1
    Sorry, my mistake was confident I had it the right way around ;)... Fixed! – Martin Sep 18 '12 at 13:53
  • 1
    After fix, your answer returns logic to the state when no traits were used. – Guy Fawkes Sep 18 '12 at 13:55
  • How does this one perform? I used a legacy constructor instead as it didn't seem to throw a collision.. http://pastebin.com/zHktB5C0 – Martin Sep 18 '12 at 13:56
  • Okay, what is an output of your code, if you'll create MyHelloWorld instance with parameters 3, 7, and 10? – Guy Fawkes Sep 18 '12 at 13:58
  • Gives 10 still... The documentation doesn't explicitly state that this will work on constructors - perhaps it isn't supported. Upvoted the question – Martin Sep 18 '12 at 14:04
  • And, in your case, you use mixin, but use Base constructor instead of mixin's constructor you used! – Guy Fawkes Sep 18 '12 at 14:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16833/discussion-between-guy-fawkes-and-martin) – Guy Fawkes Sep 18 '12 at 14:07
  • If `Base` is an ordinary class (ie. not a trait), as stated in the question, then this will result in a fatal error of the form: "Fatal error: Class Base is not a trait, Only traits may be used in 'as' and 'insteadof' statements in..." – MrWhite Jun 02 '16 at 16:30
  • `insteadof` is an abomination. Never use it. PHP creators will eventually regain their sanity, remove `insteadof`, and break your code. – Szczepan Hołyszewski Sep 30 '21 at 15:32
5

Old post but, in case this helps anyone:

I had a similar situation but decided to use a slightly different approach. I was writing a WordPress plugin and wanted to pass the plugin info around (version, name, text domain etc..) but didn't want to go around changing each file when refactoring or extend another class so I created a trait with a constructor that simply calls an init function for class specific operations.

trait HasPluginInfoTrait{
    public function __construct() { 

        $this->plugin_name        = PLUGIN_NAME;
        $this->version            = PLUGIN_VERSION;

        if ( method_exists( $this, 'init' ){
            $this->init();
        }
    }
}

class SampleClass {
    use HasPluginInfoTrait;

    private function init(){
        // Code specific to SampleClass
    }
}
  • 2
    Nice idea, but that stops making sense when adding several traits to a class, which is not uncommon. – jlh May 19 '20 at 08:03
  • 1
    Wouldn't this make more sense to have a method in the trait, like `PluginInfoInit()`, then add a call in the class constructor like: `if ( method_exists( $this, 'PlubinInfoInit' ) ) { $this->PluginInfoInit(); }` This would work with multiple traits and could just be stuck in the constructor when the trait is added. – LavaSlider Jun 07 '20 at 13:19