Bird
0
0

Which of the following is the correct way to declare a constructor method in a PHP class?

easy📝 Conceptual Q2 of 15
PHP - Classes and Objects
Which of the following is the correct way to declare a constructor method in a PHP class?
Afunction construct() {}
Bfunction __construct() {}
Cfunction _construct() {}
Dfunction __destruct() {}
Step-by-Step Solution
Solution:
  1. Step 1: Recall PHP constructor syntax

    PHP uses the magic method named __construct() as the constructor.
  2. Step 2: Compare options to correct syntax

    Only function __construct() {} matches the exact method name with double underscores.
  3. Final Answer:

    function __construct() {} -> Option B
  4. Quick Check:

    Constructor syntax = __construct() [OK]
Quick Trick: Constructor always named __construct() with two underscores [OK]
Common Mistakes:
  • Using single underscore instead of double
  • Confusing constructor with destructor __destruct()
  • Omitting underscores entirely

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes