How can the use of __sleep() and __wakeup() methods in PHP classes impact the storage of object instances in sessions?

When using the __sleep() and __wakeup() methods in PHP classes, it's important to consider how these methods can impact the storage of object instances in sessions. The __sleep() method is called before an object is serialized for storage, allowing you to unset any properties that should not be serialized. On the other hand, the __wakeup() method is called after an object is unserialized from storage, allowing you to reinitialize any properties that were unset during serialization. By properly implementing these methods, you can control what data is stored in sessions and ensure that object instances are correctly restored when retrieved.

class MyClass implements Serializable {
    private $data;

    public function __construct($data) {
        $this->data = $data;
    }

    public function serialize() {
        return serialize($this->data);
    }

    public function unserialize($data) {
        $this->data = unserialize($data);
    }

    public function __sleep() {
        return ['data'];
    }

    public function __wakeup() {
        // Perform any necessary initialization here
    }
}

// Usage
$obj = new MyClass('Hello');
$serialized = serialize($obj);
$obj = unserialize($serialized);