Bird
0
0

What is the purpose of the __destruct method in a PHP class?

easy📝 Conceptual Q11 of 15
PHP - Classes and Objects
What is the purpose of the __destruct method in a PHP class?
ATo clean up resources when an object is destroyed
BTo initialize object properties when created
CTo define a class constant
DTo handle user input validation
Step-by-Step Solution
Solution:
  1. Step 1: Understand the role of __destruct

    The __destruct method is called automatically when an object is destroyed or goes out of scope.
  2. Step 2: Identify its purpose

    It is used to clean up resources like closing files or database connections safely.
  3. Final Answer:

    To clean up resources when an object is destroyed -> Option A
  4. Quick Check:

    Destructor method = cleanup on object destruction [OK]
Quick Trick: Destructor cleans up when object is removed from memory [OK]
Common Mistakes:
  • Confusing __destruct with __construct
  • Thinking it initializes objects
  • Using it to handle input validation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes