Free cookie consent management tool by TermsFeed Generator PHP Trait | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Trait

PHP Trait

Last Updated on Mar 22, 2023

Introduction

Do you remember when we talked about inheritance?

A class can extend only one class. But what if you wanted to inherit multiple classes and have their functionalities in your current class?

The answer is Traits

Trait

We use traits to be able to solve the problem mentioned above. We define the trait like this:

<?php
Trait Example{

}

Then all the methods go inside here.

Now in the class that is going to inherit these functionalities instead of extend we use the word “use” inside the class like this;

Class Human{
    use Example;
}

Now your Human has all the functionalities defined in Example trait.

Trait Example{
    public function sayHello(){
        echo "hello World";
    }
}

Class Human {
    use Example;
}

$human = new Human();
$human->sayHello();
// hello World

You have another trait and you want to use those functionalities as well? No problem.

<?php
Trait Example{
    public function sayHello(){
        echo "hello World";
    }
}

Trait Name{
    public function sayName(){
        echo "my name is Amir";
    }
}

Trait Message{
    public function sayMessage(){
        echo "This is my message for you";
    }
}

Class Human {
    use Example,Name,Message;
}

$human = new Human();
$human->sayHello();
// hello World
$human->sayName();
// my name is Amir
$human->sayMessage();
// This is my message for you

https://youtu.be/KFfQYUsJrIM

Conclusion

Now you know about trait in PHP.

I recommend you to open a PHP files and define multiple traits. then try to use all of them in one class.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • trait in PHP
  • trait vs class
  • using mulitple traits

Category: programming

Tags: #php #oop

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

Top 5 websites to learn PHP for FREE
Dec 22, 2022 programming

Top 5 websites to learn PHP for FREE

Do you want to learn PHP but you don’t want to spend hundreds of dollars? You are not alone. There are many websites that help people like you and me to start our journey. ...

9 Min Read Read More
PHP Include and Require
Mar 21, 2023 programming

PHP Include and Require

Today we are going to learn about include and require in PHP. they help you organize your code by keeping your code in different files and use them whenever you need without duplicating your code. ...

6 Min Read Read More
Top Skills for Developers: Non-Technical and Technical Essentials
Apr 05, 2023 programming

Top Skills for Developers: Non-Technical and Technical Essentials

Being a successful developer requires a combination of both technical and non-technical skills. ...

9 Min Read Read More
Laravel 12 Beginner to Advanced: Complete Guide
Jul 17, 2025 programming

Laravel 12 Beginner to Advanced: Complete Guide

Laravel is a name that resonates throughout the PHP community. With its elegant syntax, robust ecosystem, and developer-friendly tools, Laravel has become the go-to framework for modern web development. Laravel 12, released on February 24, 2025, continues this tradition, streamlining workflows and boosting performance with support for the latest PHP advancements. ...

20 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...