Should numerical values be validated for setting variables in PHP classes, and is it necessary to throw exceptions if non-numeric values are passed?

It is a good practice to validate numerical values when setting variables in PHP classes to ensure that the data being assigned is of the correct type. If non-numeric values are passed, it can lead to unexpected behavior or errors in the code. It is recommended to throw exceptions when non-numeric values are passed to provide clear feedback to the developer.

class MyClass {
    private $number;

    public function setNumber($number) {
        if (!is_numeric($number)) {
            throw new InvalidArgumentException('Value must be numeric');
        }
        $this->number = $number;
    }
}