Bird
0
0

What happens automatically when a PHP object with a __destruct method goes out of scope?

easy📝 Conceptual Q1 of 15
PHP - Classes and Objects
What happens automatically when a PHP object with a __destruct method goes out of scope?
AThe object is cloned.
BThe <code>__construct</code> method is called again.
CThe <code>__destruct</code> method is called to clean up resources.
DThe object is serialized.
Step-by-Step Solution
Solution:
  1. Step 1: Understand object lifecycle in PHP

    When an object is no longer needed or goes out of scope, PHP automatically calls the destructor method if it exists.
  2. Step 2: Role of __destruct method

    The __destruct method is designed to free resources or perform cleanup tasks before the object is destroyed.
  3. Final Answer:

    The __destruct method is called to clean up resources. -> Option C
  4. Quick Check:

    Destructor call on object end = B [OK]
Quick Trick: Destructor runs automatically when object is destroyed [OK]
Common Mistakes:
  • Thinking destructor runs on object creation
  • Confusing destructor with constructor
  • Assuming destructor clones the object

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes