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

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
PHP session
Mar 21, 2023 programming

PHP session

Today we are going to learn about session in PHP. Sessions are like cookies but instead of storing the key value pairs on the user’s computer, session stores them on the server. ...

8 Min Read Read More
PHP Array Combine
Feb 14, 2023 programming

PHP Array Combine

Today we are going to talk about array combine in PHP. Array combine is another useful array function that creates an array by using one array for keys and another for its values. ...

3 Min Read Read More
Docker Beginner to Advanced 2025: The Complete Guide for Developers
Jul 18, 2025 programming

Docker Beginner to Advanced 2025: The Complete Guide for Developers

Containers have transformed the way we develop, deploy, and scale applications. Docker leads the charge, offering a developer-friendly platform to create consistent, portable environments from development to production. ...

27 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 ...