Menu
Coddy logo textTech

Introduction to Traits

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 51 of 91.

PHP only supports single inheritance - a class can extend just one parent class. But what if you need to share functionality across multiple unrelated classes? This is where traits come in.

A trait is a mechanism for code reuse that lets you define methods and properties that can be inserted into multiple classes. Think of traits as copy-paste at the language level - the code from a trait becomes part of the class that uses it.

<?php
trait Timestampable {
    private string $createdAt;
    
    public function setCreatedAt(): void {
        $this->createdAt = date('Y-m-d H:i:s');
    }
    
    public function getCreatedAt(): string {
        return $this->createdAt;
    }
}

class Article {
    use Timestampable;
    
    public function __construct(public string $title) {
        $this->setCreatedAt();
    }
}

$article = new Article("PHP Traits");
echo $article->getCreatedAt();

The use keyword inside a class imports the trait's members. Now Article has access to setCreatedAt() and getCreatedAt() as if they were defined directly in the class.

The same trait can be used in completely unrelated classes - a Comment class, a User class, or any other class that needs timestamp functionality. This solves the limitation of single inheritance by allowing horizontal code sharing across your class hierarchy.

challenge icon

Challenge

Easy

Let's build a simple greeting system that demonstrates how traits allow you to share functionality across unrelated classes. You'll create a trait that provides a greeting capability, then use it in two completely different classes.

You'll organize your code across three files:

  • Greetable.php — Create a trait called Greetable that provides greeting functionality. The trait should have:
    • A private property $greeting (string)
    • A method setGreeting(string $greeting) that stores the greeting message
    • A method greet() that returns the greeting message
  • Person.php — Create a Person class that uses the Greetable trait. Include the trait file and use constructor promotion to define a public $name property. In the constructor, call setGreeting() to set the greeting to "Hello, I'm [name]".
  • Robot.php — Create a Robot class that also uses the Greetable trait. Include the trait file and use constructor promotion to define a public $model property. In the constructor, call setGreeting() to set the greeting to "Beep boop, I am model [model]".

In main.php, include both class files. You'll receive two inputs: a person's name and a robot's model number. Create instances of both classes and print their greetings on separate lines.

This demonstrates the power of traits — Person and Robot are completely unrelated classes with no shared parent, yet they both gain the same greeting functionality through the trait. Each class customizes how it uses that functionality in its own way.

Cheat sheet

A trait is a mechanism for code reuse that allows you to share methods and properties across multiple unrelated classes, solving PHP's single inheritance limitation.

Define a trait using the trait keyword:

<?php
trait Timestampable {
    private string $createdAt;
    
    public function setCreatedAt(): void {
        $this->createdAt = date('Y-m-d H:i:s');
    }
    
    public function getCreatedAt(): string {
        return $this->createdAt;
    }
}

Use a trait in a class with the use keyword:

class Article {
    use Timestampable;
    
    public function __construct(public string $title) {
        $this->setCreatedAt();
    }
}

The trait's methods and properties become part of the class as if they were defined directly in it. Multiple unrelated classes can use the same trait to share functionality horizontally across your class hierarchy.

Try it yourself

<?php

require_once 'Person.php';
require_once 'Robot.php';

// Read input
$name = trim(fgets(STDIN));
$model = trim(fgets(STDIN));

// TODO: Create a Person instance with the given name
// TODO: Create a Robot instance with the given model
// TODO: Print the Person's greeting
// TODO: Print the Robot's greeting (on a new line)

?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming