I am fairly new to using PHP with classes and wondered if there is a better method for what I'm doing. Basically I am looking for the best way to handle user errors (like "That username is taken" etc).
What I am doing..
In init.php
global $errors;
$errors = array();
require ...
In classname.php
class Test {
public function myFunction($username) {
if ... {
//Do Something
global $errors;
$this->errors = $errors[] = "Username already in use!";
}
else ... {
global $errors;
$this->errors = $errors[] = "Username already in use!";
}
}
public function .... {}
}
Basically is there a way I can use the global array without having to re-write global $errors every time? Having to do repeat it just doesn't feel efficient which in my case usually means there is a better way. Any ideas?
global $errors;once at the top of classname.php.addError($str)for example) that handles the global $error array and simply call that in your class.