QUIZGUM

Coding Class

Quizgum : trait

trait

No inheritance is necessary.
You and I are growing up in a horizontal relationship, and I'm old and employed so I can organize my inheritance with my parents and stand up independently.
The expression is strange.
In order to use certain features in a class, you must maintain inheritance with the parent class.
But there is a trait that is not the inheritance, but the concept of bringing.
In other words, think of it as a concept of inserting you into me without inheriting between classes.
Another class cannot inherit from two parent classes.
Next, please check the class.

So let's look at an example to see if it can be inherited from more than one class.

  1. <?php
  2. class Parent
  3. {
  4. }
  5.  
  6. class Parent2
  7. {
  8. }
  9.  
  10. class mickey extends Parent, Prent2
  11. {
  12. }
  13.  
  14. $mickey = new mickey;
  15. ?>

As you can see, it's impossible.
Multiple traits can be used. And it's an insertion concept, not an inheritance concept.
If you're not on top of me, you're coming to me for a while.
Using traits is similar to class, but you can't create instances of traits, but you can use trait methods through classes.

How To Use trait

  1. trait traitname{}

And to use traits in a class, use the use keyword.

How to insert a trait

  1. trait traitname{}
  2. class classname
  3. {
  4. use traitname;
  5. }

Then let's declare the method in the trait and call it.

  1. <?php
  2. trait disney
  3. {
  4. public function mickey()
  5. {
  6. return "Disney's beginning Mickey Mouse";
  7. }
  8. }
  9.  
  10. class marvel
  11. {
  12. use disney;
  13. }
  14.  
  15. $marvel = new marvel;
  16. echo $marvel->mickey();
  17. ?>

You can also use multiple traits in one class.

  1. <?php
  2. trait disney
  3. {
  4. public function mickey()
  5. {
  6. return "Disney's beginning Mickey Mouse";
  7. }
  8. }
  9.  
  10. trait TokyoDisneyLand
  11. {
  12. public function duffyAndFriends()
  13. {
  14. return "We make our own character Duffy and export it to Disney in another country";
  15. }
  16. }
  17.  
  18. trait HongkongDisneyLand
  19. {
  20. public function duffyhk()
  21. {
  22. return "Thanks TDL Hong Kong children are very fond of Duffy";
  23. }
  24. }
  25.  
  26. class marvel
  27. {
  28. use disney, TokyoDisneyLand, HongkongDisneyLand;
  29. }
  30.  
  31. $marvel = new marvel;
  32. echo $marvel->mickey();
  33. echo '<br>';
  34. echo $marvel->duffyAndFriends();
  35. echo '<br>';
  36. echo $marvel->duffyhk();
  37. ?>

Of course, you can also use properties in traits.

  1. <?php
  2. trait disney
  3. {
  4. public $disney = 'disney';
  5. public function mickey()
  6. {
  7. return "Disney's beginning Mickey Mouse";
  8. }
  9. }
  10.  
  11. class marvel
  12. {
  13. use disney;
  14. }
  15.  
  16. $marvel = new marvel;
  17. echo $marvel->mickey();
  18. echo '<br>';
  19. echo $marvel->disney;
  20. ?>

Now, we've learned about traits that can be used as concepts of insertion rather than inheritance.
If you have to use a lot of things, inheritance is better than class. If you insert multiple traits and have the same method name, an error occurs.
Next time, let's find out what to do.