Bird
0
0

Identify the error in this PHP code:

medium📝 Debug Q6 of 15
PHP - Interfaces and Traits
Identify the error in this PHP code:
trait Sample { public function test() { return "test"; } } class Demo { use Sample } $obj = new Demo(); echo $obj->test();
AClass Demo must extend trait Sample
BTrait method 'test' is private
CMissing semicolon after 'use Sample'
DCannot instantiate class using trait
Step-by-Step Solution
Solution:
  1. Step 1: Check trait usage syntax

    When using a trait inside a class, the statement must end with a semicolon.
  2. Step 2: Locate error

    The code misses a semicolon after use Sample in class Demo.
  3. Final Answer:

    Missing semicolon after 'use Sample' -> Option C
  4. Quick Check:

    Trait usage requires semicolon after 'use' statement [OK]
Quick Trick: Add semicolon after 'use TraitName' in class [OK]
Common Mistakes:
  • Omitting semicolon after 'use' statement
  • Trying to extend a trait
  • Assuming trait methods are private by default

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes