Bird
0
0

Which of the following is the correct syntax to declare a trait in PHP?

easy📝 Syntax Q12 of 15
PHP - Interfaces and Traits
Which of the following is the correct syntax to declare a trait in PHP?
Aabstract trait Logger {}
Binterface Logger {}
Ctrait Logger {}
Dclass Logger {}
Step-by-Step Solution
Solution:
  1. Step 1: Recall PHP syntax for traits

    Traits are declared using the keyword trait followed by the trait name and curly braces.
  2. Step 2: Eliminate incorrect options

    abstract trait Logger {} is invalid because traits cannot be abstract. interface Logger {} declares an interface, and class Logger {} declares a class.
  3. Final Answer:

    trait Logger {} -> Option C
  4. Quick Check:

    Trait declaration uses 'trait' keyword [OK]
Quick Trick: Traits start with 'trait' keyword, no 'abstract' needed [OK]
Common Mistakes:
  • Adding 'abstract' before trait keyword
  • Confusing trait with interface or class syntax
  • Using 'class' keyword for traits

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes