Bird
0
0

Why might a PHP class use a private constructor method?

hard📝 Conceptual Q10 of 15
PHP - Classes and Objects
Why might a PHP class use a private constructor method?
ATo make the constructor run only once per script
BTo allow multiple constructors with different names
CTo prevent direct object creation and control instantiation
DTo automatically call the destructor
Step-by-Step Solution
Solution:
  1. Step 1: Understand private constructor purpose

    Private constructors stop code outside the class from creating objects directly.
  2. Step 2: Recognize use cases like singleton pattern

    This control allows the class to manage how and when objects are created.
  3. Final Answer:

    To prevent direct object creation and control instantiation -> Option C
  4. Quick Check:

    Private constructor = control object creation [OK]
Quick Trick: Private constructor restricts object creation outside class [OK]
Common Mistakes:
  • Thinking private constructor allows multiple constructors
  • Confusing constructor with destructor behavior
  • Believing private constructor runs only once automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes