What are the best practices for structuring and organizing data retrieval and manipulation within PHP classes to avoid variable overwriting?

When structuring and organizing data retrieval and manipulation within PHP classes, it is important to avoid variable overwriting by properly scoping variables within methods. One way to prevent variable overwriting is by using class properties to store data that needs to persist across different methods. Additionally, using meaningful and unique variable names can help avoid unintentional overwriting.

class DataHandler {
    private $data = [];

    public function setData($key, $value) {
        $this->data[$key] = $value;
    }

    public function getData($key) {
        return $this->data[$key] ?? null;
    }
}

// Example usage
$dataHandler = new DataHandler();
$dataHandler->setData('name', 'John');
$dataHandler->setData('age', 30);

echo $dataHandler->getData('name'); // Output: John
echo $dataHandler->getData('age'); // Output: 30