QUIZGUM

Coding Class

Quizgum : trait static

Calling methods in a trait without instantiation

Did you call a method of a class without instantiating it using the static keyword?
Yes, this is possible if the trait's methods also use static, which is called method static invocation.

How to use static in a trait method

  1. trait traitname{
  2. access modifier static function method name(){}
  3. }

So let's look at an example.

  1. <?php
  2. trait apple
  3. {
  4. public static function phone()
  5. {
  6. return 'iPhone';
  7. }
  8. }
  9.  
  10. class people
  11. {
  12. use apple;
  13. }
  14.  
  15. echo "Judith in zootopia ".people::phone()." Use it.";
  16. ?>

Result

Traits can also set rules. The rules you declared in the trait must be declared in the class.
See you next time.