<!--
title: Use object methods consistently
domain: app-frameworks
topic: Code Style
language: PHP
source: appwrite/appwrite
updated: 2025-07-20
url: https://awesomereviewers.com/reviewers/appwrite-use-object-methods-consistently/
-->

When working with Document objects, always use their accessor methods rather than array-style syntax. Use `$object->getAttribute('property')` and `$object->setAttribute('property', $value)` instead of `$object['property']`. This ensures proper encapsulation, maintains data integrity through validation, and creates more resilient code that won't break if the underlying implementation changes.

```diff
-// Avoid direct array access on Document objects
-if (isset($session['clientName']) && empty($session['clientName'])) {
-    $session['clientName'] = !empty($session['userAgent']) ? $session['userAgent'] : 'UNKNOWN';
-}

+// Prefer method access for Document objects
+if ($session->getAttribute('clientName', null) === null || empty($session->getAttribute('clientName'))) {
+    $clientName = !empty($session->getAttribute('userAgent')) ? $session->getAttribute('userAgent') : 'UNKNOWN';
+    $session->setAttribute('clientName', $clientName);
+}
```

This pattern ensures your code leverages the Document object's built-in validation and transformation features while providing better readability and maintainability.
