Recall & Review
beginner
What problem do traits solve in PHP?
Traits help solve the problem of code reuse when multiple classes need the same functionality but do not share a parent class.
Click to reveal answer
beginner
How do traits differ from inheritance in PHP?
Traits allow you to include methods in multiple classes without using inheritance, avoiding the limitations of single inheritance.
Click to reveal answer
beginner
Why can't interfaces replace traits?
Interfaces only define method signatures but do not provide method implementations, while traits provide reusable method code.
Click to reveal answer
beginner
Can traits help avoid code duplication? How?
Yes, traits let you write common methods once and reuse them in many classes, reducing repeated code.Click to reveal answer
beginner
What is a real-life example of why traits are useful?
Imagine many different animals that can all 'make sound'. Instead of repeating the 'make sound' method in each animal class, a trait can provide it once and be used by all.
Click to reveal answer
What is the main reason to use traits in PHP?
✗ Incorrect
Traits allow code reuse across classes that do not share a parent, which inheritance alone cannot do.
Which of these can traits provide that interfaces cannot?
✗ Incorrect
Traits provide actual method code, while interfaces only declare method names.
Traits help avoid which common programming problem?
✗ Incorrect
Traits let you write code once and reuse it, reducing repeated code.
Can a class use multiple traits in PHP?
✗ Incorrect
PHP allows a class to use multiple traits to combine different sets of methods.
Which feature does PHP lack that traits help to overcome?
✗ Incorrect
PHP does not support multiple inheritance, so traits provide a way to reuse code from multiple sources.
Explain why traits are needed in PHP and how they help with code reuse.
Think about how you can share the same method in many classes without inheritance.
You got /4 concepts.
Describe a simple example where using a trait is better than inheritance or interfaces.
Imagine different animals making sounds but not related by inheritance.
You got /4 concepts.