This question already has an answer here:
- Accessing variables through classes 1 answer
I have the following class:
class validationHandler{
private $dataType; //set via constructor ...
private $validation = null;
private function requireValidation(){
if($this->validation == null){
$this->validation = loadDataFromJSONfile($this->dataType);
}
}
public function validate($data){
$this->requireValidation();
//validate... the rules are in the $validation-variable
}
}
When an object needs to validate some data, it makes a new validation-object and calls validate().
The first time something needs to be validated, the data is loaded from a json-file.
But: Sometimes I have several Objects which need the same validation-file. And This code loads the file for each Object.
Question: Is there a way to set $validation global, so that every object accesses the same variable, but without loosing the private-property?
(Notice that I have different validation-files and different kinds of objects. An objects tells the validationHanlder in __construct() which type it has, and which validation-file should be loaded. So I need $validation to be an array. (Didn't write it in the code, so it's more readable)
</div>