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:
Step 1: Recall PHP syntax for traits
Traits are declared using the keyword trait followed by the trait name and curly braces.
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.
Final Answer:
trait Logger {} -> Option C
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
Master "Interfaces and Traits" in PHP
9 interactive learning modes - each teaches the same concept differently