Should form data be cleaned outside of a class or within the class constructor in PHP?

Cleaning form data within the class constructor is generally a better practice as it keeps the logic encapsulated within the class itself. This helps in maintaining a clean and organized codebase. It also ensures that the data is cleaned consistently every time an instance of the class is created.

class FormDataProcessor {
    private $formData;

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

    private function cleanFormData($data) {
        // Perform data cleaning operations here
        return $data;
    }
}

// Example usage
$formData = $_POST; // Assuming form data is submitted via POST
$processor = new FormDataProcessor($formData);