I have this class
class Validator implements iValidation{
protected
$_fields,
$_errors;
public function __construct($fields){
$this->_errors = array();
$this->_fields = $fields;
}
public function validate(){
$map = unserialize( iValidation::map );
foreach ($this->_fields as $field_type => $data){
if ( array_key_exists( $field_type, $map ) ){
$class = "Validate_{$map["$field_type"]}" ;
$object = new $class($data);
$object->validate();
unset($object);
}
}
}
public function getErrors(){
return $this->_errors;
}
}
Now this class loops through an array given in the format
$admin_test_data = array(
"firstname" => "Alex1",
"surname" => "Morley-Finch",
"username" => "alex123",
"password" => "cheese",
"re_password" => "cheese",
"email_address" => "alex54353hotmail.co.uk",
"user_type" => "ADMIN",
"add_admin" => "Add Admin",
);
I have a map that describes the type of validation on each field that is declared like so:
define(
"VALIDATION_MAP",
serialize(
array(
// FIELD => VALIDATION TYPE
"firstname" => "Name",
"surname" => "Name",
"agency_name" => "Agency_Name",
"agency_office" => "Name",
"username" => "Username",
"email_address" => "Email_Address",
"password" => "Password",
)
)
);
interface iValidation{
public function __construct($data);
public function validate();
const map = VALIDATION_MAP;
}
And I have sub classes One of which is like this:
class Validate_Name extends Validator implements iValidation{
private $_data;
public function __construct($data){
$this->_data = $data;
}
public function validate(){
$data = $this->_data;
$length = strlen($data);
if ( $length > 40 ){
$this->_errors[] = "Cannot be more than 40 characters";
}
if ( $length < 3 ){
$this->_errors[] = "Cannot be less than 3 characters";
}
if ( !preg_match("/^[a-zA-Z]+$/", $data) ){
$this->_errors[] = "A-Z characters only";
}
}
}
And the whole framework is used like so:
$validator = new Validator($admin_test_data);
$validator->validate(); // ^ defined earlier ^^^
$errors = $validator->getErrors();
Now after all that background info… lets get to the problem.
I’m probably doing some EXTREMELY stupid but I can’t figure it out and Google isn’t doing a good job of enlightening me ( or I’m not doing a good job of asking google )
The sub classes reference $this->_errors[] = "This is the error"; and when using die( var_dump( $this->_errors ) ); inside the sub class, the errors for that class appear as they should.
However if I call die( var_dump( $validator->getErrors() ) ); outside the classes, the array is empty.
Asif the parent has forgotten all the values the children set?
Whats going on?
For each field you validate in
Validator, you create an instance of the required implementation ofiValidation.Now the child validator validates, and it finds errors. It adds those errors to its
_errorsinstance attribute.When it’s done validating, the child instance is discarded. And that’s where the problem lies, since the errors found were assigned to the child instance’s
_errorsattribute, not that of the parent instance.Nothing ever gets assigned to
Validator::_errors, because those properties are in different objects.