PHP theorytheory 0/50 · 0%
OOP · easy

15. Traits

Reusing method implementations.

Traits let you share method implementations across unrelated classes without using inheritance, addressing PHP's single-inheritance limitation. A class pulls in a trait with the use keyword inside the class body.

When multiple traits define conflicting methods, PHP requires explicit conflict resolution using 'insteadof' and 'as'.

Check your understanding

  1. 1. How does a class include a trait?

  2. 2. What resolves trait method name conflicts?