简体   繁体   中英

Call to an overrided trait method

I would like to use traits to instanciate my objects with my DIC:

trait TUseContainer {

    protected $c;

    public function __construct(Container $c) {
        $this->c=$c;
    }

}

class MyClass {
    use TUseContainer;

    //Optional
    public function __construct(ClassInheritedFromContainer $c){
        TUseContainer::__construct($c);
        //MyClass __construct stuff
    }

}

So my questions are:

  1. Does TUseContainer::__construct($c); will work?
  2. If not, does parent::__construct($c); will do the trick? (I think it will not)
  3. Is trait::myOverridedMethod(); a good way to call non static overrided method?
  4. Do you think I should use the "as" keyword? (I think it's a bad idea)
  5. Is "TUseContainer" a good name for what I intend to do?

All coments are welcome, thanks.

I'll do some tests and post results.

I got some trivial workaround:

trait TUseContainer {

    protected $c;

    public function __construct(Container $c) {
        $this->setContainer($c);
    }

    protected function setContainer(Container $c){
        $this->c=$c;
    }
}

class MyClass {
    use TUseContainer;

    //Optional
    public function __construct(ClassInheritedFromContainer $c){
        $this->setContainer($c);
        //MyClass __construct stuff
    }

}
  1. No
  2. No
  3. No
  4. No
  5. Open

All coments are still welcome

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM