Jump to content

OOP Revisited


TheFilmGod

Recommended Posts

<?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.

Link to comment
https://forums.phpfreaks.com/topic/172437-oop-revisited/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.