What alternatives exist for storing parts of an object in PHP instead of the entire object?

When dealing with large objects in PHP, it may be inefficient to store the entire object in memory. One alternative is to store only the necessary parts of the object that are needed for a specific operation. This can help reduce memory usage and improve performance.

// Example: Storing only specific parts of an object

class User {
    public $id;
    public $username;
    public $email;

    public function __construct($id, $username, $email) {
        $this->id = $id;
        $this->username = $username;
        $this->email = $email;
    }
}

// Create a new User object
$user = new User(1, 'john_doe', 'john@example.com');

// Store only the necessary parts of the User object
$userData = [
    'id' => $user->id,
    'username' => $user->username
];

// Access the stored data
echo $userData['id']; // Output: 1
echo $userData['username']; // Output: john_doe