Creating a Webpage using OO PHP

Thread Solved

Join Date: Feb 2008
Posts: 5
Reputation: geordiejon is an unknown quantity at this point 
Solved Threads: 0
geordiejon geordiejon is offline Offline
Newbie Poster

Creating a Webpage using OO PHP

 
0
  #1
Oct 21st, 2008
I am trying to get to grips with Object Oriented PHP. Last week we were given a exercise to do, which creates a webpage, using a class and a client which will call the class. I am totaly not getting the hang of this, and i am getting so frustraited with this. We are moving on to the next lesson this week, and i am worried i am going to fall behind, because i just cannot figure it out. Webpage.class.php holds the webpage class, where all the functions ect are held, and then i am supposed to use the client usewebpage.php to create a new webpage, but a week of trying, i have got nowhere, and i am hitting a brick wall trying to figure out how to do it.

Is there any OO php peeps out there who could give me a hand please? I am in a mess.

Thank You.


Webpage.class.php
  1. <?php
  2.  
  3. /**
  4.  *
  5.  * This is a skeleton class for a Webpage class. We have given you the method names
  6.  * and a very brief comment on what those methods need to do. You will need to implement the class yourself.
  7.  *
  8.  * Also, included with this file is a file that is a 'client' or 'user' of the class.
  9.  * If you look at the way the client is using the class it'll give you some more clues about how
  10.  * you might need to implement the methods that the client calls.
  11.  *
  12.  */
  13.  
  14. /**
  15. * The class should be called: Webpage
  16. * you will need attributes to hold at least the main sections of a page: the head, body and footer
  17. */
  18. Class Webpage {
  19.  
  20. private $head;
  21. private $addToBody;
  22.  
  23. function __construct($head, $addToBody) {
  24. // Store the doctype into the attribute of the class
  25. $this->head = $head;
  26. $this->addToBody = $addToBody;
  27. //$this->styles = array() ??????;
  28.  
  29. }
  30.  
  31. function addToBody($toAdd){
  32. $this->addToBody .= $toAdd;
  33. return;
  34. }
  35.  
  36. function getPage(){
  37. return $this->head . $this->addToBody;
  38. }
  39.  
  40. }
  41. /** Methods:
  42. * A constructor
  43. * The constructor for the class should accept at least two arguments: the title of the page, and an array of
  44. * css filenames that it will use to create the appropriate code in the head section to link to those stylesheets
  45. * The constructor should create the head section and footer section and give a default value for the body section
  46. *
  47. * addToBody
  48. * a method called 'addToBody' that will add text to the body attribute of the webpage. See the client to see how this method
  49. * will be used - it'll give you a clue as to how to implement it.
  50. *
  51. * getPage
  52. * a getPage method which has as a return value the various sections, head, body and footer, of the webpage concatenated together.
  53. *
  54. * Consider carefully the scope of all attributes and methods/functions. Remember to make scope as restrictive
  55. * as possible while still being consonant with the class working.
  56. *
  57. */
  58.  
  59. ?>

usewebpage.php
  1. <?php
  2.  
  3. /**
  4.  *
  5.  * @version $Id$
  6.  * @copyright 2008
  7.  */
  8. // require the class definition file
  9. require_once( 'webpage.class.php' );
  10.  
  11. // create a new instance of the webpage class passing the title and an array of stylesheets to the constructor
  12. $page = new Webpage ("test", array( "dream.css", "main.css" ) );
  13.  
  14. // add new text to the page body
  15. $page->addToBody( "\n<p>This is my first page constructed with my webpage class</p>" );
  16. $page->addToBody( "\n<p>Look a the source html of this page and notice the doctype, the title and stylesheets are all correctly included and the code should validate as xhtml</p>");
  17. $page->addToBody( "<div id="header"><img src="images/needs2007_small.png" />");
  18. $page->addToBody( "<h1>Header</h1>");
  19. $page->addToBody( "</div>");
  20.  
  21. // echo the page contents back to the browser
  22. echo $page->getPage();
  23.  
  24.  
  25. ?>
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Creating a Webpage using OO PHP

 
0
  #2
Oct 21st, 2008
is the code on usewebpage.php theirs or yours?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: geordiejon is an unknown quantity at this point 
Solved Threads: 0
geordiejon geordiejon is offline Offline
Newbie Poster

Re: Creating a Webpage using OO PHP

 
0
  #3
Oct 21st, 2008
Originally Posted by kkeith29 View Post
is the code on usewebpage.php theirs or yours?
usewebpage.php is their code, except for lines 17 & 19. They have shown us, what it is supposed to look like.

If it helps, i have tried to do a little more, and i am getting somethings done, but not alot

this is what i have tried so far.

  1. Class Webpage {
  2.  
  3. private $head;
  4. private $addToBody;
  5. private $cssFiles;
  6.  
  7. function __construct($head, $addToBody, $cssFiles) {
  8. // Store the doctype into the attribute of the class
  9. $this->head = $head;
  10. $this->addToBody = $addToBody;
  11. $this->styles = $cssFiles;
  12. //$this->styles = array() ??????;
  13.  
  14. }
  15.  
  16. function addToBody($toAdd){
  17. $this->addToBody .= $toAdd;
  18. return;
  19. }
  20.  
  21. function styles($styles){
  22. $this->styles .= $styles;
  23. //echo "<link href=\"css/dream.css\" rel=\"stylesheet\" type=\"text/css\" />";
  24. return;
  25. }
  26.  
  27. function getPage(){
  28. return $this->head . $this->addToBody . $this->styles;
  29. }
  30.  
  31. }

  1. <?php
  2.  
  3. /**
  4.  *
  5.  * @version $Id$
  6.  * @copyright 2008
  7.  */
  8. // require the class definition file
  9. require_once( 'webpage.class.php' );
  10.  
  11. // create a new instance of the webpage class passing the title and an array of stylesheets to the constructor
  12. $page = new Webpage ($head, $addToBody, $cssFiles);
  13.  
  14. // add new text to the page body
  15. //$page->head("<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">);
  16. //$page->head ("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
  17. //$page->head ("<head>");
  18. //$page->head ("<meta http-equiv=\"Content-Type\" content=\"text/html\; charset=\utf-8\" />");
  19. //$page->head ("<title>Home</title>");
  20. $page->styles("<link href=\"css/project.css\" rel=\"stylesheet\" type=\"text/css\" />");
  21. $page->addToBody( "\n<p>This is my first page constructed with my webpage class</p>" );
  22. $page->addToBody( "\n<p>Look a the source html of this page and notice the doctype, the title and stylesheets are all correctly included and the code should validate as xhtml</p>");
  23. $page->addToBody( "<div id=\"open\"><img src=\"images/needs2007_small.png\" />");
  24. $page->addToBody( "<h1>Header</h1>");
  25. $page->addToBody( "</div>");
  26.  
  27. // echo the page contents back to the browser
  28. echo $page->getPage();
  29.  
  30.  
  31. ?>
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Creating a Webpage using OO PHP

 
0
  #4
Oct 21st, 2008
what are you trying to accomplish. are you just suppose to show that you can use it or do you actually need to create a certain webpage.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: geordiejon is an unknown quantity at this point 
Solved Threads: 0
geordiejon geordiejon is offline Offline
Newbie Poster

Re: Creating a Webpage using OO PHP

 
0
  #5
Oct 22nd, 2008
Originally Posted by kkeith29 View Post
what are you trying to accomplish. are you just suppose to show that you can use it or do you actually need to create a certain webpage.
To create XHTML webpages, not just one single page. So the webpage class can be reused over and over, for new pages, with only small changes needed in the usewebpage.php.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 26
Reputation: vl4kn0 is an unknown quantity at this point 
Solved Threads: 4
vl4kn0's Avatar
vl4kn0 vl4kn0 is offline Offline
Light Poster

Re: Creating a Webpage using OO PHP

 
0
  #6
Oct 22nd, 2008
In my opinion is much better tu use something like this:

Template.class.php:

  1. <?php
  2.  
  3. class Template
  4. {
  5. private $tpl_data = array();
  6. private $file;
  7.  
  8. function __construct($filename)
  9. {
  10. // Load file into $file var
  11. $this->file = implode('', @file($filename));
  12. }
  13.  
  14. public function assign($vardata)
  15. {
  16. // Load var array into $tpl_data
  17. foreach($vars as $key => $value) {
  18. $this->tpl_data[$key] = $value;
  19. }
  20. }
  21.  
  22. public function run()
  23. {
  24. // replace {$key} into $value
  25. foreach($this->tpl_data as $key => $value) {
  26. $this->file = str_replace('{' . $key . '}', $value, $this->file);
  27. echo $this->file;
  28. }
  29. }
  30. }
  31.  
  32. ?>

template.html

  1. <html>
  2. <head>
  3. <title>{TITLE}</title>
  4. </head>
  5. <body>
  6. <h1>{WELCOME}</h1>
  7. </body>
  8. </html>

show_page.php

  1. <?php
  2.  
  3. $tpl = new Template("template.html");
  4. $tpl->assign(array(
  5. 'TITLE' => 'Just a test',
  6. 'WELCOME' => 'Welcome on our pages'
  7. ));
  8. $tpl->run();
  9.  
  10. ?>
Last edited by vl4kn0; Oct 22nd, 2008 at 3:39 am.
Keep in mind that I'm not native english speaker....
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: geordiejon is an unknown quantity at this point 
Solved Threads: 0
geordiejon geordiejon is offline Offline
Newbie Poster

Re: Creating a Webpage using OO PHP

 
0
  #7
Oct 22nd, 2008
Originally Posted by vl4kn0 View Post
In my opinion is much better tu use something like this:

Template.class.php:

  1. <?php
  2.  
  3. class Template
  4. {
  5. private $tpl_data = array();
  6. private $file;
  7.  
  8. function __construct($filename)
  9. {
  10. // Load file into $file var
  11. $this->file = implode('', @file($filename));
  12. }
  13.  
  14. public function assign($vardata)
  15. {
  16. // Load var array into $tpl_data
  17. foreach($vars as $key => $value) {
  18. $this->tpl_data[$key] = $value;
  19. }
  20. }
  21.  
  22. public function run()
  23. {
  24. // replace {$key} into $value
  25. foreach($this->tpl_data as $key => $value) {
  26. $this->file = str_replace('{' . $key . '}', $value, $this->file);
  27. echo $this->file;
  28. }
  29. }
  30. }
  31.  
  32. ?>

template.html

  1. <html>
  2. <head>
  3. <title>{TITLE}</title>
  4. </head>
  5. <body>
  6. <h1>{WELCOME}</h1>
  7. </body>
  8. </html>

show_page.php

  1. <?php
  2.  
  3. $tpl = new Template("template.html");
  4. $tpl->assign(array(
  5. 'TITLE' => 'Just a test',
  6. 'WELCOME' => 'Welcome on our pages'
  7. ));
  8. $tpl->run();
  9.  
  10. ?>
Hi, thanks that does look good, and seems to be a much simpilar way of creating the pages.

I have followed your example, and added __ require_once 'Temp.Class.php'; to the showpage.php, but it comes up with the following error : -

Warning: Invalid argument supplied for foreach() in C:\wamp\www\testing\Temp.Class.php on line 17

Correction: -- I have spotted the error, $vars should be $vardata
Last edited by geordiejon; Oct 22nd, 2008 at 4:00 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 26
Reputation: vl4kn0 is an unknown quantity at this point 
Solved Threads: 4
vl4kn0's Avatar
vl4kn0 vl4kn0 is offline Offline
Light Poster

Re: Creating a Webpage using OO PHP

 
0
  #8
Oct 22nd, 2008
Ou. Yes of course should be $vardata. Thanks for correction. But I can't edit my post after 30 minutes
Last edited by vl4kn0; Oct 22nd, 2008 at 4:11 am.
Keep in mind that I'm not native english speaker....
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC