How can the use of setters and getters improve the design and functionality of a PHP class for handling measurement values?

Using setters and getters in a PHP class for handling measurement values allows for better control over the data being set and retrieved. This helps in maintaining data integrity and encapsulation within the class, as well as providing a way to implement validation and formatting logic for the measurement values.

class Measurement {
    private $value;

    public function setValue($value) {
        // Add validation logic here, for example:
        if (!is_numeric($value)) {
            throw new Exception("Value must be a number");
        }
        $this->value = $value;
    }

    public function getValue() {
        return $this->value;
    }
}

// Example usage:
$measurement = new Measurement();
$measurement->setValue(10);
echo $measurement->getValue(); // Output: 10