Bird
0
0

Which of the following is the correct syntax to define a constructor in a PHP class?

easy📝 Syntax Q3 of 15
PHP - Inheritance and Polymorphism
Which of the following is the correct syntax to define a constructor in a PHP class?
Afunction __construct() {}
Bfunction constructor() {}
Cfunction ClassName() {}
Dfunction __destruct() {}
Step-by-Step Solution
Solution:
  1. Step 1: Recall PHP constructor syntax

    PHP constructors are defined using the special method named __construct().
  2. Step 2: Identify incorrect options

    constructor() is not a PHP constructor, ClassName() is old style and deprecated, __destruct() is for destructors.
  3. Final Answer:

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

    Constructor syntax = __construct() [OK]
Quick Trick: Use __construct() for constructors in PHP classes [OK]
Common Mistakes:
  • Using old style constructor named after class
  • Confusing destructor with constructor
  • Using generic function names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes