TheFilmGod Posted August 30, 2009 Share Posted August 30, 2009 <?php class basicValidator { // Assume no errors public $form_valid = 1; // If an error was found child function will call this public function errorFound() { $this -> form_valid = '0'; } } class validateFirstName extends basicValidator { // Class properties public $value; public $msg; public $valid = 0; // Validate first name on construct function __construct($value) { // Validate first name if ( !empty($value)) { $regex = "/^[A-Za-z][A-Za-z ]{2,49}$/"; if ( preg_match($regex, $value)) { $this -> valid = 1; $this -> msg = 'Valid!'; } else { $this -> msg = 'The name must be between 3-20 characters long. Only letters are considered as valid characters.'; } } else { $this -> msg = 'The first name was left blank.'; } // If error found, update basic form validation to 0 if (($this -> valid) == 0) { echo 'here'; parent::errorFound(); } } } // Test $first_name = 'Greg23'; $page_validator = new basicValidator(); $validation_first_name = new validateFirstName($first_name); echo $validation_first_name -> valid; echo $page_validator -> form_valid; ?> The page prints out "here01". I was expecting "here00". The second '0' signifies that the whole page validated incorrectly. The function to change that value is ErrorFound() that was called in the child class, validateFirstName. Quote Link to comment Share on other sites More sharing options...
trq Posted August 30, 2009 Share Posted August 30, 2009 Your missing the whole inheritance logic. You have an instance of basicValidator which is in no way related to your instance of validateFirstName. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.