What are the best practices for handling and displaying array data in PHP classes?

When handling and displaying array data in PHP classes, it is important to ensure that the data is properly sanitized and validated to prevent security vulnerabilities. It is also recommended to use methods within the class to access and manipulate the array data, rather than directly accessing the array properties. Additionally, consider implementing error handling to gracefully handle any issues that may arise when working with array data.

class ArrayHandler {
    private $data = [];

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

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

    public function displayData() {
        foreach ($this->data as $key => $value) {
            echo $key . ': ' . $value . '<br>';
        }
    }
}

// Example usage
$arrayHandler = new ArrayHandler();
$arrayHandler->setData(['name' => 'John', 'age' => 30]);
$arrayHandler->displayData();