Bird
0
0

Which of the following is the correct way to declare an interface in PHP?

easy📝 Syntax Q12 of 15
PHP - Interfaces and Traits
Which of the following is the correct way to declare an interface in PHP?
Aclass MyInterface { public function doSomething() {} }
Binterface MyInterface { public function doSomething(); }
Cinterface MyInterface() { function doSomething(); }
Dinterface MyInterface { function doSomething() {} }
Step-by-Step Solution
Solution:
  1. Step 1: Recall PHP interface syntax

    Interfaces use the keyword 'interface' followed by the name and method declarations without bodies.
  2. Step 2: Check method declaration rules

    Methods in interfaces must be declared without code (no braces), only the signature.
  3. Final Answer:

    interface MyInterface { public function doSomething(); } -> Option B
  4. Quick Check:

    Interface methods have no body [OK]
Quick Trick: Interface methods have no code blocks {} [OK]
Common Mistakes:
  • Adding method bodies inside interfaces
  • Using parentheses after interface name
  • Declaring interfaces as classes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes