Courses Job Ready Fresher Training AI for Class 7 to 12 Corporate Training Placements Tutorials
Free Learning Resources

IT Tutorials & Interview Prep

Free guides, interview Q&As, and job responsibility breakdowns — curated by industry veterans to help you crack MNC interviews

109+
Tutorial Articles
9
Topic Categories
100%
Free to Read
← Back to PHP

Traits

PHP Last Updated: Oct 17, 2025

Traits

PHP language only supports single-level inheritance where a child class can inherit only from one single parent. If we need to inherit multiple behaviours from a class, then we can use Traits to solve this.

Traits are used to declare methods that can be used in multiple classes which can have any access modifier (public, private, protected). Traits can also have normal methods and abstract methods that can be used in multiple classes.

Traits are declared with the trait class and to use a trait in a class we need to use the use keyword.

Example:

<?php
trait message1 {
   public function msg1() {
       echo "give me cheeseburgers ";
   }
}

class Welcome {
   use message1;
}

$obj = new Welcome();
$obj->msg1();
?>

Output:

give me cheeseburgers

Want to practice these scenarios in a real IT environment? Our 45-Day Internship Program gives you hands-on experience with Active Directory, Ticketing Tools, and Networking.
Join the Internship →