Wednesday, March 18, 2026 — A new question every day
Intermediate
0 developers answered today
What is a PHP trait and when would you use it?
💡 Hint: Think about code reuse without inheritance
✅ Answer
A trait is a mechanism for code reuse in single-inheritance languages. It lets you include methods in multiple classes without inheritance. Use when: multiple unrelated classes need the same method, you want to avoid deep inheritance trees, or you need horizontal code sharing. Define with trait keyword, use with use inside a class.