Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

megaman's avatar

traits in laravel

My understanding of PHP traits are they are like snippets of code which can be inserted using a use statement inside of a class and the effect is as if you had copy/pasted the code in at the point. The same as if you use include() or require()

so why is it that some of the laravel API documentation pages list traits associated with classes? example https://laravel.com/api/5.6/Illuminate/Database/Eloquent/Model.html

listing methods and properties makes sense because these things are a part of an object. traits however are not

0 likes
2 replies
rawilk's avatar

Traits do become part of the object when they are used in a class. It's helpful that the documentation lists them because each of these traits has additional methods and properties that are available to the class. Without listing them, you wouldn't know about these methods and properties.

4 likes

Please or to participate in this conversation.