O'Reilly logo

PHP Quick Scripting Reference by Mikael Olsson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

CHAPTER 16

image

Traits

A trait is a group of methods that can be inserted into classes. They were added in PHP 5.4 to enable greater code reuse without the added complexity that comes from allowing multiple-inheritance. Traits are defined with the trait keyword followed by a name and a code block. The naming convention is the same as for classes, with each word initially capitalized. The code block may only contain static and instance methods.

trait PrintFunctionality{  public function myPrint() { echo 'Hello'; }}

Classes that need the functionality that a trait provides can include it with the use keyword followed by the trait’s name. The trait’s ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required