PHP 7 interview question · Most asked

What are traits and when were they introduced?

Summary

Traits (PHP 5.4) are reusable sets of methods and properties that are copied into classes with use, providing horizontal code reuse without inheritance.

Longer Answer

They're useful for cross-cutting behavior (timestamps, soft deletes in Eloquent). Overuse leads to hidden dependencies, because traits often assume host-class properties. Conflicts are resolved with insteadof and as.

Key Points

  • Compile-time copy-paste into classes
  • Not a type, so can't type-hint
  • Can declare abstract methods to require host members

More most-asked PHP 7 questions

← All PHP 7 questions