Hi all,
I've been working with PHP, using procedural methods, for several years now, and have begun to explore the object-oriented side of it. While it didn't take me long to understand the potential advantages that come with working in this manner, I'm still having some trouble wrapping my head around doing the things I used to do procedurally in an object-oriented manner. A lot of the tutorials cover the basic applications of classes, properties, and methods, but I'm having trouble applying them to projects I'm working on.
For instance, I have a form, that I put a class together to validate it:
class formValidator {
//set your properties for the class
private var $user_input = array();
protected var $response_code = 0;
protected var $response_msg = "";
public function set_validator($user_input){
foreach ($user_input as $key => $value){
//check to see if the field name contains "e-",
//designating it as a required field
if(strpos($key,"r-") !== FALSE) {
//Check string length, if 0 then return empty field message
if (strlen($value) === 0) {
$this->response_code = 1;
$this->response_msg = "Required field is empty!";
}
}
//check to see if the field name contains "e-",
//designating it as an e-mail field
if(strpos($key,"e-") !== FALSE){
//Check the datatype to confirm a string has been passed in
if (is_string($value)&&($value != "")) {
//Regular expression pattern.
//Pattern breakdown:
//** [a-zA-Z0-9_] - any character between a-z, A-Z or 0-9
//** + - require one or more of the preceding item.
//** @{1} - Simply means 1 '@' symbol required.
//** [a-zA-Z]+ - any character between a-z, A-Z (1 or more required).
//** \.{1} - Single '.' required. Backslash escapes the '.'
//** [a-zA-Z]+ - One or more of the these characters require
//get rid of spaces in the field
$value = str_replace(' ','',$value);
$pattern = "/^[a-zA-Z0-9_]+@{1}[a-zA-Z]+\.{1}[a-zA-Z]+/";
//If the regex pattern does not match the value,
//set error code and error message
if (!(preg_match($pattern, $value))) {
$this->response_code = 1;
$this->response_msg .= "<BR>Entered e-mail address is not valid!";
}
}
}
}
}
public function get_validator(){
return $this->response_code;
return $this->response_msg;
}
Which I instantiate on the page, after a form is submitted:
if ($_POST["submit"]) {
$validator = new formValidator();
//$message = new message(); (gonna use this later...at least I plan to)
// validate the form
$validator->set_validator($_POST);
$validator->get_validator();
}
Now, based on the validation status of the form, I want to create a class that echoes the message resulting from the validation, however, I'm not sure how to get the results from the validator class passed to a "message" class (I'm also trying to keep the 'message' class versatile enough to handle more than just form validation - I'd like to use it to display results of a successful form submission, user alerts, etc (I think that follows the concept of 'loose coupling', correct?).
I hope I've accurately described (as well as provided functional code to describe) my situation, here, and I welcome any feedback or suggestions you guys may have out there...
-Jay