php traits - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of php traits.php

  1. <?php
  2.  
  3. trait Cafe
  4. {
  5.         public function getBeverage()
  6.         {
  7.                 return 'coffee';
  8.         }
  9. }
  10.  
  11. class Person1
  12. {
  13.         use Cafe;
  14. }
  15.  
  16. class Person2
  17. {
  18.         use Cafe;
  19.  
  20.         public function getBeverage()
  21.         {
  22.                 return 'tea';
  23.         }
  24. }
  25.  
  26. echo (new Person1())->getBeverage();
  27. echo (new Person2())->getBeverage();
  28.  
File Description
  • php traits
  • PHP Code
  • 28 Oct-2020
  • 267 Bytes
You can Share it: