PHP trait Keyword

PHP try Keyword
PHP throw Keyword

In this article, you will learn about the trait keyword in PHP. The trait keyword in PHP is used to create traits. Traits are a way to allow classes to inherit multiple behaviors.

examples of the TRAIT keyword

Example 1. In this example, we create a trait and use it in a class.

<?php
trait message1 {
  public function msg1() {
    echo "OOP is fun! ";
  }
}

class Welcome {
  use message1;
}

$obj = new Welcome();
$obj->msg1();
?>
PHP try Keyword
PHP throw Keyword
en English
X
Scroll to Top