After recently reading up on and watching videos on OO PHP programming I have decided to convert my existing site into OO to gain some experience.
I have successfully (well, I think) converted a page into OOP and would like to know whether this is considered the proper way to implement OOP and whether this should be the way I complete the webpage or whether I need to rethink my design.
Here are the included files, a validate class file, and the "view" I guess, editedbooking.php. (In editedbooking.php a config file is included, which includes validate.php and database.php (my database connection file))
My class file - validatetest.php
<?php
class Validations {
public $date2;
public $typeofnumber;
public $phonenumber;
private function validatemobile($mobnumber) {
$pattern =
'/^[\(]?(\d{0,4})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{3})$/';
if (preg_match($pattern, $mobnumber, $matches)) {
// we have a match, dump sub-patterns to $matches
$number = $matches[0]; // original number
$a_code = $matches[1]; // 4-digit area code
$exchange = $matches[2]; // 3-digit exchange
$ending = $matches[3]; // 3-digit number
// $extension = $matches[4]; // extension
$this->phonenumber = "(".$a_code.") ".$exchange." ".$ending;
} else {
throw new Exception("Please Enter a valid Telephone Number");
}
}
private function validatehome($number){
$pattern =
'/^[\(]?(\d{0,2})[\)]?[\s]?[\-]?(\d{4})[\s]?[\-]?(\d{4})$/';
if (preg_match($pattern, $number, $matches)) {
// we have a match, dump sub-patterns to $matches
$number = $matches[0]; // original number
$a_code = $matches[1]; // 2-digit area code
$exchange = $matches[2]; // 4-digit exchange
$ending = $matches[3]; // 4-digit number
// $extension = $matches[4]; // extension
$this->phonenumber = "(".$a_code.") ".$exchange." ".$ending;
} else {
throw new Exception("Please Enter a valid Telephone Number");
}
}
//method to check argument is set
public function is_set($arg1) {
if (!isset($arg1) && ($arg1 == '')) {
throw new Exception("Value {$arg1} is not set");
}
}
//determine whether number is mobile or home phone
public function determine_number($number) {
if ($number === "mobile") {
$this->typeofnumber = "mobile";
} elseif ($number === "home") {
$this->typeofnumber = "home";
}
}
//the method below validates a numbers length, length is set to 10 chars by default.
public function validatelength($arg1) {
if ($this->typeofnumber == "mobile") {
if (strlen($arg1) != 10) {
throw new Exception("Your Mobile number is not the right length!");
}
} else if ($this->typeofnumber == "home"){
if ((strlen($arg1) <= 7) || (strlen($arg1) >= 11)) {
throw new Exception("The input \"$arg1\" is not the required length!");
}
} else {
//error handling in here later
}
}
//this method has to be passed after method "determinenumber"
public function validatenumber($phonenumber) {
if ($this->typeofnumber == "mobile") {
$this->validatemobile($phonenumber);
} elseif ($this->typeofnumber == "home") {
$this->validatehome($phonenumber);
} else {
throw new Exception("Please input a number type!");
}
}
public function get_number() {
return $this->phonenumber;
}
public function date_validate($date) {
$fixingdate = $_REQUEST['date5'];
$fixingdate = explode("-", $fixingdate);
$year = $fixingdate[0];
switch ($fixingdate[1]) {
case 1:
$month = "Janurary";
break;
case 2:
$month = "Febuary";
break;
case 3:
$month = "March";
break;
case 4:
$month = "April";
break;
case 5:
$month = "May";
break;
case 6:
$month = "June";
break;
case 7:
$month = "July";
break;
case 8:
$month = "August";
break;
case 9:
$month = "September";
break;
case 10:
$month = "October";
break;
case 11:
$month = "November";
break;
case 12:
$month = "December";
break;
} // end of switch
$month2 = $fixingdate[1]; //for different query (bloew)
$day = $fixingdate[2];
$subdate = "The ".$day."th of ".$month." ".$year;
$this->date2 = "$year-$month2-$day";
return $subdate;
}
public function servicemethod($service) {
switch ($service) {
case 1:
$service = "Hair Extension - Consultation";
break;
case 2:
$service = "Hair Extension - Application";
break;
case 3:
$service = "Spray Tan";
break;
case 4:
$service = "Eyelash Extensions";
break;
}
return $service;
}
}
$validate = new Validations();
"view" page - editedbooking.php
<?php
require_once('calendar/classes/tc_calendar.php');
include ('includes/headerBK.html');
require_once('classes/config.php');
if ($session->is_logged_in() == TRUE) { //main condition - if session isnt set then user isnt logged in.
$name = ucfirst($_SESSION['first_name']);
$userid = $_SESSION['user_id'];
$vars = $_SESSION; //variables passed from login page to sesh var.
if(isset($_POST['submitted'])) {
try {
//validate the required fields are set
$validate->is_set($_POST['phone']);
$validate->is_set($_POST['numtype']);
$validate->is_set($_POST['date5']);
$validate->is_set($_POST['optone']);
$validate->is_set($_POST['opttwo']);
//determine the type of number being inputted i.e home or mobile
$validate->determine_number($_POST['numtype']);
//check the length of the entered phone number and date (just for an extra precaution)
$validate->determine_number($_POST['numtype']);
$validate->validatelength($_POST['phone']);
//validate number (has to run after "$validate->determinenumber")!
$validate->validatenumber($_POST['phone']);
//return the inputted items to a variables.
$phone = $validate->get_number();
$subdate = $validate->date_validate($_POST['date5']);
$service = $validate->servicemethod($_POST['optone']);
$location = ($_POST['opttwo']);
} catch(Exception $e) {
echo output_errormsg($e->getMessage() . "\n");
}
if ($location && $phone && $subdate) {
$database->autocommit(FALSE); //turn off mysqli auto commit
$query1 = $database->use_db("INSERT INTO booking_info (location, ph_number) VALUES ('$location', '$phone')");
if ($query1 == TRUE) {
$query2 = $database->use_db("INSERT INTO bookings (user_id, date, pending) VALUES ($userid, '$validate->date2', 'yes')");
if ($query2 == TRUE) {
$finishpage = TRUE;
$database->commit();
} else {
$database->rollback();
}
} else {
//$database->rollback();
}
// first query - adding booking_info
} else {
echo output_errormsg("There was a problem with your request");
}//END OF LOCATION PHONE AND DATE CONDITIONAL
} //END OF MAIN SUBMITTED CONDITIONAL
if (isset($finishpage)) { //if this is set then user has succesffully submitted the form already
echo "Thank you $name, $subdate has been submitted for approval!";
include('includes/footer.html');
die();
}
} else { //redirect as not logged in - MAIN SESSION CONDITIONAL
echo 'You are not logged in. Please click <a href="login.php">here</a>';
include('includes/footer.html');
die();
} //end of session conditional*/
?>
<script language="javascript">
<!--
function setOptions(chosen)
{
var selbox = document.myform.opttwo;
selbox.options.length = 0;
if (chosen == " ") {
selbox.options[selbox.options.length] = new Option('Please select one of the options above first',' ');
}
if (chosen == "1") {
selbox.options[selbox.options.length] = new Option('On-Site','onsite');
selbox.options[selbox.options.length] = new Option('Mobile','mobile');
}
if (chosen == "2") {
selbox.options[selbox.options.length] = new Option('On-Site','onsite');
selbox.options[selbox.options.length] = new Option('Mobile','mobile');
}
if (chosen == "3") {
selbox.options[selbox.options.length] = new Option('On-Site','onsite');
selbox.options[selbox.options.length] = new Option('Mobile','mobile');
}
if (chosen == "4") {
selbox.options[selbox.options.length] = new Option('On-Site','onsite');
selbox.options[selbox.options.length] = new Option('Mobile','mobile');
}
}
-->
</script>
<img src="images/topwrapper.png" id="righttopimage"/>
<h1>Book Online</h1>
<div id="contactform">
<form name="myform" action="editedbooking.php" method="post">
<fieldset><legend>Enter your information in the form below : </legend>
<P><B>Contact Number:</b><input type="text" name="phone" size="40" maxlength="20" /></p>
<p><b>Home Phone<input type="radio" name="numtype" value="home" /> Mobile<input type="radio" name="numtype" value="mobile" /></b></p>
</p>
<table border="0"><tr><td><p><b>Preferred Time : </td><td><?php
$myCalendar = new tc_calendar("date5", true, false);
$myCalendar->setIcon("calendar/images/iconCalendar.gif");
//$myCalendar->setDate(date('d'), date('m'), date('Y'));
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(2000, 2015);
$myCalendar->dateAllow('2008-05-13', '2015-03-01');
$myCalendar->setDateFormat('j F Y');
//$myCalendar->setHeight(350);
//$myCalendar->autoSubmit(true, "form1");
$myCalendar->setAlignment('left', 'bottom');
$myCalendar->setSpecificDate(array("2011-04-01", "2011-04-04", "2011-12-25"), 0, 'year');
$myCalendar->setSpecificDate(array("2011-04-10", "2011-04-14"), 0, 'month');
$myCalendar->setSpecificDate(array("2011-06-01"), 0, '');
$myCalendar->writeScript();
?>
</b></p></td></tr></table>
<p><b>Please select the service you require : </b></p>
<select name="optone" size="1" onchange="setOptions(document.myform.optone.options[document.myform.optone.selectedIndex].v alue);">
<option value=" " selected="selected"></option>
<option value="1">Hair Extension - Consultation</option>
<option value="2">Hair Extensions - Application</option>
<option value="3">Spray Tan</option>
<option value="4">Eyelash Extensions</option>
</select><br /> <br />
<select name="opttwo" size="1">
<option value=" " selected="selected">Please select one of the options above first</option>
</select>
<input type="hidden" name="submitted" value="1">
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>
</form>
</div>
<?php
include ('includes/footer.html');
?>