Problem Statement
Differentiate between constructor and destructor in PHP.
Explanation
A constructor (__construct) runs automatically when an object is created, often to initialize values. A destructor (__destruct) runs when an object is destroyed or the script ends, used for cleanup like closing database connections.
Example:
class FileHandler {
function __construct(){ echo 'Opening file'; }
function __destruct(){ echo 'Closing file'; }
}
Code Solution
SolutionRead Only
class Demo { function __construct(){} function __destruct(){} }