Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔴 Advanced  ·  Lesson 23

Traits in PHP (with Examples)

In this lesson: Learn PHP traits — syntax, multiple traits, conflict resolution and when to use traits vs inheritance — with examples.

The problem traits solve

In PHP a class can extends only one parent. But sometimes two unrelated classes need the same helper methods — say a logging feature. Copy-pasting it everywhere is bad. A trait solves this: define the shared methods once, then pull them into any class with use.

💡 A trait is like a reusable "mix-in" of methods you can drop into any class.

Defining a trait

<?php
  trait Logger {
      public function log($msg) {
          return "LOG: " . $msg;
      }
  }
?>

A trait looks like a class but uses the trait keyword. You cannot create an object from it — it exists only to be pulled into real classes.

Using a trait

<?php
  class Order {
      use Logger;   // pull in the trait's methods
  }
  $o = new Order();
  echo $o->log("Order placed");
?>
LOG: Order placed

The use Logger; line copies the trait's log() method into Order, as if it were written there. Any class can do the same, without inheritance.

Combining multiple traits

A class can pull in several traits at once.

<?php
  trait Logger    { public function log($m){ return "LOG: $m"; } }
  trait Timestamp { public function now(){ return "2026-01-01"; } }

  class Report {
      use Logger, Timestamp;
  }
  $r = new Report();
  echo $r->log("done") . " at " . $r->now();
?>
LOG: done at 2026-01-01

This is where traits shine — you mix in abilities from several sources, something single inheritance cannot do.

Trait vs interface

PointTraitInterface
Contains code?Yes (real methods)No (names only)
PurposeShare an implementationEnforce methods exist
Keyworduseimplements

Use a trait to share ready code; use an interface to require that certain methods exist. They are often used together.

Common mistakes

  • Trying to instantiate a trait. You cannot do new Logger() — traits go inside classes.
  • Ignoring method-name conflicts. Two traits with the same method need insteadof/as to resolve.
  • Overusing traits. Too many mixed-in methods make a class hard to follow — use them purposefully.
  • Confusing traits with interfaces. Traits carry code; interfaces do not.

Summary

  • A trait is reusable method code you pull into classes with use.
  • It solves PHP's single-inheritance limit for shared code.
  • A class can use several traits at once.
  • Traits carry real code; interfaces only list method names.
  • Resolve same-name method conflicts with insteadof/as.

Next, enforce method contracts with abstract vs interface, or review classes in PHP OOP.

Frequently Asked Questions

What is a trait in PHP?
A trait is a group of methods you can reuse in several classes without inheritance. You define the methods once in a trait, then pull them into any class with the use keyword. It is PHP's answer to reusing code across unrelated classes.
Why do we need traits if we have inheritance?
PHP allows a class to extend only one parent. If you need shared code from two different sources, inheritance cannot do it. Traits solve this by letting a class pull in methods from several traits at once.
What is the difference between a trait and an interface?
A trait contains real method code that gets copied into the class. An interface only lists method names with no code. Use a trait to share an implementation; use an interface to enforce that methods exist.
Can a class use more than one trait?
Yes. A class can use several traits at once by listing them: use TraitA, TraitB;. Their methods all become available in that class, which is a clean way to mix in different abilities.
What happens if two traits have the same method name?
That causes a conflict, and PHP asks you to resolve it using the insteadof and as keywords to choose or rename which method to keep. It prevents silent, confusing overrides.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicAbstract Class vs Interface Your progress: 23 / 50 (46%) · Lesson 24 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free