I'm trying to validate user data input using PHP OOP and display errors if they exist. However, this doesn't seem to be working.
This is my class:
class Forms {
private int $age;
private int $year;
private $output = array();
public $displayErrors = array();
function getAge() {
if (is_int($this->age)) {
return $this->age;
} else {
//$this->age = 'null';
$this->errorMsgs = array('error' => 'The output is not a number',);
return $this->errorMsgs;
}
}
function setAge($age) {
$this->age = $age;
}
public function displayErrors() {
$displayErrors[] = Forms::getAge()->$errorMsgs;
return $displayErrors;
}
}
If $age
is not a number I want it to display the error The output is not a number
through an array. and return the array when displayErrors
is called.
However I'm getting this error: Notice: Undefined variable: errorMsgs
I tried modelling my code on this reply.