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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.