PHP OOPLesson 52Free preview
PHP Traits
This lesson is part of PHP · All levels
Summary
Trait is a set of methods that can be inserted into several different classes with the use keyword, thus overcoming the limitations of single inheritance in PHP. Traits like a piece of code shared that copies its method to the user class. If there is a conflict of method names, the class can choose which one to use with the keyword insteadof.
Key points
- Trait allows sharing methods to multiple classes without inheritance.
- The keyword use in the class is used to include the trait.
- Traits cannot be instantiated with new and not referred to as parent.
- The method of collision is solved by insteadof and renaming using as.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.