Bird
0
0

Identify the error in the following PHP code using traits:

medium📝 Debug Q14 of 15
PHP - Interfaces and Traits
Identify the error in the following PHP code using traits:
trait Logger {
    public function log() {
        echo 'Logging';
    }
}

class User {
    use Logger
    public function create() {
        echo 'User created';
    }
}
AClass cannot use traits
BMissing semicolon after <code>use Logger</code>
CTrait cannot have public methods
DMethod <code>create</code> must be static
Step-by-Step Solution
Solution:
  1. Step 1: Check trait usage syntax

    When using a trait inside a class, the use statement must end with a semicolon.
  2. Step 2: Identify missing semicolon

    The code misses a semicolon after use Logger, causing a syntax error.
  3. Final Answer:

    Missing semicolon after use Logger -> Option B
  4. Quick Check:

    Trait use requires semicolon [OK]
Quick Trick: Add semicolon after trait use in class [OK]
Common Mistakes:
  • Omitting semicolon after trait use
  • Thinking traits can't have public methods
  • Assuming methods must be static

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes