What Is a Trait?

This section describes what is a trait - A trait is a pseudo class that defines a set of properties and operations. A trait can not be instantiated to create new objects. It can only be 'used' by one or more classes for the purpose of code sharing.

What Is a Trait? A trait is a pseudo class that defines a set of properties and operations. A trait can not be instantiated to create new objects. It can only be "used" by one or more classes for the purpose of code sharing.

In PHP, a trait is declared with the "trait" statement block. classes can share the code defined in a trait with the "use" statement. For example, the following tutorial example provides two traits "MySize" and "MyType" and one class "MyImage" that uses both traits.

<?php /* MyImage.php
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
*/

trait MySize {
   protected $width;
   protected $height;
   public function showSize() {
      echo "   Size: " . $this->width .", ". $this->height ."\n";
   }
}
trait MyType {
   public $type;
   public function showType() {
      echo "   Type: " . $this->type ."\n";
   }
}

class MyImage {
   use MySize;
   use MyType;
   protected $image;
   public function __construct($w,$h,$t) {
      $this->width = $w;
      $this->height = $h;
      $this->type = $t;
      $this->image = imagecreatetruecolor($this->width,$this->height); 
   }
   public function showImage() {
      ob_start();
      imagepng($this->image);
      $buffer = ob_get_contents();
      ob_end_clean();   	
      echo "   Image: " . base64_encode($buffer) ."\n";
   }
}

  $object = new MyImage(8,8,"JPEG");
  echo "MyImage object: \n";
  $object->showSize();
  $object->showType();
  $object->showImage();
?>

If you run the above tutorial example, you will get something like this:

MyImage object:
   Size: 8, 8
   Type: JPEG
   Image: iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAA
DElEQVQImWNgGB4AAADIAAH4XVuUAAAAAElFTkSuQmCC

Last update: 2015.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.0

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Configuring and Sending out Emails

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

Introduction of Class and Object

 What Is a Class?

 What Is an Object?

 What Is a Constructor?

 What Is a Static Method?

 What Is a Static Variable?

 What Is a Superclass and a Subclass?

 What Is an Abstract Class?

 What Is an Abstract Method?

 What Is an Interface?

What Is a Trait?

 What Is an Overloaded Property?

 What Is an Overloaded Method?

 What Is Object Property Iteration?

 What Is Object Cloning?

 What Is Object Serialization?

 What Is in an Object Variable?

 Updating Variables Who Share an Object

 Passing Objects as Function Arguments

 Integrating PHP with Apache Web Server

 Outdated Tutorials

 References

 PDF Printing Version