PHP OOPLesson 46Free preview
PHP Destructor
This lesson is part of PHP · All levels
Summary
Destructor is a __destruct method that PHP runs when an object is no longer in use or when the script is finished. Destructor is useful for clearing resources such as closing files, connections, or database cursors. If the class does not define the destructor, PHP simply releases the object memory without additional action.
Key points
- Destructor is written as __destruct and runs automatically when the object is destroyed.
- Destructor is useful for clearing open resources in the constructor.
- The destructor execution sequence follows the time the object is released, not the manufacturing sequence.
- A class can only have one destructor.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.