What is the difference between __construct and __destruct in PHP?
__construct is a magic method in PHP that is called when an object is created, while __destruct is a magic method that is called when an object is destroyed. __construct is used to initialize an object's properties or perform any setup tasks, while __destruct is used to perform cleanup tasks before an object is destroyed.
class MyClass {
public function __construct() {
echo 'Object created';
}
public function __destruct() {
echo 'Object destroyed';
}
}
$myObject = new MyClass();
unset($myObject);