What role do magic methods like __sleep() and __wakeup() play in the serialization process in PHP?
Magic methods like __sleep() and __wakeup() play a crucial role in the serialization process in PHP. The __sleep() method is called before the object is serialized, allowing you to specify which properties should be serialized. On the other hand, the __wakeup() method is called after the object is unserialized, allowing you to reinitialize any necessary resources.
class MyClass {
public $data;
public function __sleep() {
return ['data'];
}
public function __wakeup() {
// Reinitialize any necessary resources
}
}