PHP trait Keyword

❮ PHP Keywords

Example

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();
?>
Try it Yourself »

Definition and Usage

The trait keyword is used to create traits. Traits are a way to allow classes to inherit multiple behaviours.


Related Pages

Read more about traits in our PHP OOP - Traits Tutorial.


❮ PHP Keywords
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.