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

PHP Loops

Today we are going to learn about loops in PHP. Loop is another important concept in programming and we are going to learn everything about loops in PHP. ...

11 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 simple REST API
Feb 15, 2023 programming

PHP simple REST API

Today we are going to create a simple REST API in PHP. we are going to do it with pure PHP and with no frameworks. you can create a RESTful API in 70 lines of code. ...

25 Min Read Read More
AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers
Jul 18, 2025 programming

AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers

In today’s web development landscape, AngularJS may no longer dominate headlines, but it still powers countless legacy applications. Many organizations—especially in finance, healthcare, and enterprise software—rely on AngularJS systems that require ongoing maintenance and enhancements. ...

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