-Mike- Posted June 27, 2007 Share Posted June 27, 2007 Right, so OOP isn't exactly new to me. However, applying it to a website is kind of new, and I'm wondering if I am doing this correctly or not. Is it un-necessary, have I got "good practice" going (sorry for some layout inconsistencies, tried to make it so you don't have to scroll horizontally to see all content! - all code works) Basically, I am not sure about it. I'm hoping that someone has the patience to sit through reading it all. The application is not important at this time, just a concept idea - throwing together something like a concept at this point. The idea is that people will visit a web page, enter their post code into a form, and click "okay". This will then: 1) Check the post code is valid. If not, redisplay the page with their entered details, and also what is wrong with their codes (incorrect format etc) 2) Check whether the (valid) post codes are covered. If so, then it outputs a message that they have coverage by re-presenting the page with this message and the form with their code in again. or Checks the code, finds no coverage, and outputs the page again with an apology message, but the option to receive an update when the coverage is in their area. Anyway, two classes to give an example. A "Page" class, and a "Coverage" class. There are more classes involved, header is just the page header s- like <html><title> $title</title> etc Footer is the same, but with a default footer message, menu has a default html menu. Database is just a query method, abstracts the connection responsibilty and actual method of query to the class, so that its type (mysql) can be changed without affecting any other class. <?php require_once 'Header.class.php'; require_once 'Footer.class.php'; require_once 'Menu.class.php'; /** * Class representing the page, although the page has NOT been abstracted down into classes representing the actual content boxes, or * sides of the page themselves... just a basic representation. * Could do with some extra work in the near future. * */ class Page{ var $header; //Holds html content for a page header, including meta tags var $left; //The left side of the page overall. var $right;//the right side of the page. var $leftbox;//array for items to display in left. var $rightbox;//array for items to display on the right. var $main;//The main. var $footer; //the footer message to the page. var $breadcrumb; //the pages breadcrumb. function Page(){ //Footer, static class method returns default footer for every page. $this->footer = Footer::$footer; / $this->leftbox = array(); $this->rightbox = array(); } /* * @param $title, the title of the page to be displayed, displayed in the browser bar * @param $metaKey, the meta keywords for the page to be displayed. Default value available. * @param $metaDesc, the meta description words for the page to be displayed. * * */ function setHeaders($title = "", $metaKey = "", $metaDesc = ""){ //Create a new Header object, with the given title, meta description and keywords. $head = new Header($title, $metaKey, $metaDesc); //Set the header to be the HTML text returned by the getHeaders function $this->header = $head->getHeaders(); } /* * Function to set the breadcrumbs value. * @param $crumb - the html code of links for the breadcrumb. * */ function setBreadCrumb($crumb){ $this->breadcrumb = $crumb; } /* * Allows the user to add a content box to the right hand side of the page. * Will add the page format for content, so into a rounded edged "box" on the page. * @param $content - the html content of the right hand side "box" to be added */ function addRightBox($content){ $this->rightbox[] = '<div class="top"></div><div class="middle">' . $content . '</div><div class="bottom"></div>'; } /* * Allows to add a header to the right hand side page, which will not have the content box added * @param $content - just the text to be displayed as a title on the page, without any box formatting. */ function addRightTitle($content){ $this->rightbox[] = '<div class="title">' . $content . '</div>'; } /* * Allows the user to add a content box to the left hand side of the page. * Will add the page format for content, so into a rounded edged "box" on the page. * @param $content - the html content of the left hand side "box" to be added * @param $logo - the class (text) value for the box top (basically allows to change logos to pre-determined css ones at will) */ function addLeftBox($content, $logo = ""){ if($logo == ""){ //if no logo specified, go with the default. $this->leftbox[] = '<div class="topthin"></div><div class="middle">'. $content . '</div><div class="bottom"></div>'; } else { //otherwise, allow the top to display the corresponding logo. $this->leftbox[] = '<div class="'.$logo.'"></div><div class="middle">' . $content . '</div><div class="bottom"></div>'; } } /* * Allows the user to add something to the left hand side as it is. * This should only be used when the code for the format is added * Otherwise, addLeftBox should be used to maintain the layout as required. */ function addLeft($content){ $this->leftbox[] = $content; } /** * Function allows to add errors to an error box on the left side of the page. * @param $content - the basic html formatted text for the errors. */ function addErrorLeftBox($content){ $this->leftbox[] = '<div class="topError"></div><div class="middleError"> ' . $content . '</div><div class="bottomError"></div>'; } /* * This should ONLY be used when the layout is controlled by the calling class * (as in FAQ, left side layout is dependant upon the page * specifically, and therefore it has it's own layout defined). * Otherwise, it should be controlled by using the addLeftBox function. */ function setLeft($content){ $this->left = $content; } /* * This should ONLY be used when the layout for the right hand side is controlled by the calling class entirely. * Otherwise, the addRightBox function should be used. * */ function setRight($content){ $this->right = '<div id="right"><div id="breadcrumb">' . $this->breadcrumb . '</div>' . $content . '</div>'; } /* * Creates the "left" div of the page, by iterating over all the boxes contained, * outputting the overall left hand side. * Essentally wraps all "leftbox" items into the left div, ready for display. * Once done, cannot be undone. */ private function compileLeft(){ $this->left = '<div id="left">'; foreach($this->leftbox as $key => $value){ $this->left .= $value; } $this->left .= '</div>'; } /* * Creates the "right" div of the page, by iterating over all the boxes contained * outputting the overall right hand side. */ private function compileRight(){ $this->right = '<div id="right"><div id="breadcrumb">' . $this->breadcrumb . '</div>'; foreach($this->rightbox as $key => $value){ $this->right .= $value; } $this->right .= '</div>'; } /* * Compiles the two sides of the page, dependant upon whether the setLeft/setRight functions * have been used, * or whether the addBox has been used. In instances where both used, * the set functions will have priority (ie over-ride) * the add functions... */ private function compilePage(){ if(!isset($this->left)){ $this->compileLeft(); } if(!isset($this->right)){ $this->compileRight(); } $this->setMain($this->left . $this->right); } /* * Function sets the main content of the page. Assigns the top menu, and inserts page * contents between the content divs * Adds the banner and menu to the main page. * Constructs the page body, the left and right contents are added together in a content div * which is contained within the main div. * The menu is also added to the page body at this time. */ private function setMain($main){ $this->main = '<body><div id="main">' . Menu::$topMenu . '<div id="content">' . $main . '</div>'; } /* * Compiles the page, and returns the html to be output. */ function returnPage(){ $this->compilePage(); return $this->header . $this->main . $this->footer; } } ?> The coverage class, which utilises the page class, along with database classes (just has a query and a cleaning function, whereby does a mysql_real_escape_string on the passed variable, and returns it). <?php //Require Database class, as per usual require_once 'Database.class.php'; //The validating class require_once 'Validator.class.php'; //The class that creates pages. require_once 'Page.class.php'; /** * Evaluates whether a supplied post code is covered or not in the service. * Will check postcodes using the validator class. * Constructs a page utilising the Page class, setting values for the parts of the page. * * */ class Coverage { var $left; //The left side of the page. var $right;//The right side of the page. var $title;//Page title. var $connection;//The db reference here. var $validator; //The validator reference here. var $breadcrumb; //html for the breadcrumb. var $page; //reference to the page object. function Coverage($first = null, $second = null){ //initialise the few objects required first - the page and database connection. $this->page = new Page; $this->connection = new Database; $this->page->setHeaders("Coverage", "", ""); $this->page->addRightBox('Text'); //Set the breadcrumb to have a link to the home page, this is default, others may be added //later (wrapped in the page class) $this->breadcrumb = '<a href="index.php" title="Home">Home</a> > '; //Check if there are some parts to the post code. if(isset($first) && isset($second)){ //We need a validator, construct one now. $this->validator = new Validator; //Validate the entered post code parts $er1 = $this->validator->validateFirstPostcode($first); $er2 = $this->validator->validateSecondPostcode($second); //Were there any errors in the validator (indicating errors with the codes). if($this->validator->numErrors() > 0){ //There were errors in the checking, so we need to re-display with the errors //displayed somewhere... //Use the validators "getDisplayableErrors" (html formatted error messages), //and add them to the left side of page. $this->page->addLeftBox($this->validator->getDisplayableErrors(), "error"); //Display back the input box, wih the values entered by user. $this->page->addLeftBox($this->getInputBox($first, $second, $er1, $er2), "coverage"); //Set some right side of the page text. $this->page->addRightBox("Just sample text"); } else { //No errors found, all okay - query the db to find a match for their entry. $num = $this->getResultCode($first, $second); //returned value is 0, then no matches. If it's any other number - //it's the number they contact... if($num){ //If there is a "num" set, then the post code entered is covered, //they need to contact this number sent. $this->page->addLeftBox($this->getMessage($num), "covered"); } else { //if there is not, then we don't have cover. Display the "coming soon" //version instead (pre-set message). $this->page->addLeftBox($this->getMessage($num), "soon"); } //Add the normal input box to the page, so they can check another post code //(includes their entered values). $this->page->addLeftBox($this->getInputBox($first, $second), "coverage"); } } else { //No post code submission existed, so display the default page instead. $this->page->addLeftBox($this->getInputBox(), "coverage"); } } /** * Method to add to the breadcrumb... * */ function breadCrumb($crumb){ $this->breadcrumb .= $crumb; } /* * Function returns html text of the input box for users to input their post code details. * @return $input - the HTML text that will construct the input box required. */ function getInputBox($first = "", $second = "", $er1="1", $er2="1"){ $input = '<div class="inputbox"><form id="form2" name="form2" method="post" action="coverage-map.php"> <div class="title"><b>Am I Covered?</b></div> <span class="label"><label>Postcode:</label></span> <span class="divs"><input class="input'.$er1.'" name="First" type="text" id="First" size="2" maxlength="4" value="'.$first.'"/> <input type="text" class="input'.$er2.'" name="Second" id="Second" size="1" maxlength="3" value="'. $second .'" /></span> <span class="divs"><input name="IntSub" type="submit" id="IntSub" value="Check" /></span> </form></div>'; return $input; } /** * Method takes the number given, and according to whether it's a 0 (false) or a positive value, *will * then alter the message that gets returned. * */ function getMessage($num){ if($num){ $str = "Coverage available, text your postcode to " . $num . " to initiate registration process"; } else { $str = "Coverage is not yet available in your area. Text your postcode to 656565 to be alerted when coverage is available"; } return $str; } /* * Function that takes both parts of a post code (should have been pre-checked for *correctness on format) * The postcode used will be checked against the database to check coverage. * @param $first - the first section of postcode * @param $second - the second part of the postcode. * @return $num - numerical value representing the number users will text to sign up on. * If the postcode wasn't covered, will return 0. */ function getResultCode($first, $second){ //Set up the return value to be 0 at the start (also evaluates to false in a if statement, //which is why chosen) $num = 0; //Firstly, "clean" the input to avoid sql injection attempts. $first = $this->connection->clean(strtoupper(trim($first))); $second = $this->connection->clean(strtoupper(trim($second))); //Run query with the cleaned input $result = $this->connection->query("SELECT * FROM PostcodeMapping WHERE PostcodeArea = '" . $first ."'"); //Check if the result shows we cover this area. if($this->connection->numRows($result) > 0){ //If covered, we need to obtain the code number that people will need to text into the business. //Get the value that the users will have to text. while($res = $this->connection->fetchArray($result)){ $num = $res['TextNum']; } } //Now to return the value that was found. This will either be the mobile phone number found by the query, or alternatively it will be the //value of 0, which will indicate that the postcode wasn't covered. return $num; } /** * Calls the Page objects returnPage method, which will return a string of the html of the page. * @return String - string of the page's html to output. */ function returnPage(){ return $this->page->returnPage(); } } ?> Essentially, when you visit the "coverage.php", this is it's code: //we require the specific classes require_once 'classes/Coverage.class.php'; $coverage = new Coverage($_POST['First'], $_POST['Second']); echo $coverage->returnPage(); Creates the Coverage class, passing the "post" variables (may or may not exist of course). Echo's out the html of the page the class makes. So, my query is whether I am actually doing things in a reasonably effective manner, or whether my attempted use of Objects in the design is both poor and weak. I know I can do things proceedural - but if I wanted to do proceedural, I would have used proceedural. I want to have it OO, and now I wish to find out whether I'm doing okay, or poorly. ANY suggestions or hints/tips are really appreciated, as is your time if still reading at this point Tried searching these things via Google, but tutorials tend to be of vastly differing quality. Furthermore, classes may already exist for some things I am trying to do, but I am trying to learn myself, not just learn how to use others stuff - but get good principles etc. Bit long winded there, apologies Link to comment https://forums.phpfreaks.com/topic/57400-good-design-bad-designpractice-are-my-objects-okay/ Share on other sites More sharing options...
php_forum Posted July 18, 2007 Share Posted July 18, 2007 Hi , Previously,I have created a php application which displayed rssfeeds taken from specific sites. The Rss Feed is displayed without any problem . Then I moved my system to a specific domain.Then,I changed the proxy settings & port in Internet options(IE). Now the RSS Feed is not coming ... what is the code we have to add for the proxy settings in php... Please give me a solution .. Thanks Link to comment https://forums.phpfreaks.com/topic/57400-good-design-bad-designpractice-are-my-objects-okay/#findComment-301054 Share on other sites More sharing options...
keeB Posted July 18, 2007 Share Posted July 18, 2007 @php_forum... *Boggle* lol. -Mike-: I think what you have is really readable code. Those comments, WOW! Never seen so many!! If this is the only page you need to create, then I think what you have is perfect. When you get in to multiple pages you may have some design flaws, but I think it suits this page quite well. Link to comment https://forums.phpfreaks.com/topic/57400-good-design-bad-designpractice-are-my-objects-okay/#findComment-301277 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.