Home > Back-end >  Trait has same static method as my class - how to resolve?
Trait has same static method as my class - how to resolve?

Time:11-01

In my class I am using a trait called PlankMediable that has a method public static function bootMediable().

Unfortunately, my class also has a method called protected static function bootMediable().

  1. How do I alias the class as once is public static and the other is protected static? Will this work?
use PlankMediable  {
    PlankMediable::bootMediable as PlankMediable_bootMediable;
}
  1. How do I call the static method from my classes protected static function bootMediable() class?

CodePudding user response:

Yes, you can define an alias with use. Take a look at the following example:

trait SayWorld {
    public static function sayHello() {
        echo __METHOD__." from trait";
    }
}

class MyHelloWorld {
    use SayWorld{
     SayWorld::sayHello as traithello;
    }

    public static function sayHello() {
        echo __METHOD__." from class";
    }

    public static function callFromTrait() {
        self::traithello();
    }
}

    MyHelloWorld::traithello();  //SayWorld::sayHello from trait
    MyHelloWorld::sayhello();  //MyHelloWorld::sayHello from class
    MyHelloWorld::callFromTrait();  //SayWorld::sayHello from trait
  •  Tags:  
  • php
  • Related