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

<?php

/**
 *
 * This is a skeleton class for a Webpage class.  We have given you the method names
 * and a very brief comment on what those methods need to do.  You will need to implement the class yourself.
 *
 * Also, included with this file is a file that is a 'client' or 'user' of the class.
 * If you look at the way the client is using the class it'll give you some more clues about how
 * you might need to implement the methods that the client calls.
 *
 */

/**
* The class should be called: Webpage
* you will need attributes to hold at least the main sections of a page: the head, body and footer
*/
Class Webpage {

	private $head;
	private $addToBody;

	function __construct($head, $addToBody) {
    // Store the doctype into the attribute of the class
        $this->head = $head;
        $this->addToBody = $addToBody;
        //$this->styles = array() ??????;

        } 

	function addToBody($toAdd){
   		$this->addToBody .= $toAdd;
   		return;
		}

	function getPage(){
  		return $this->head . $this->addToBody;
		}

	}
/** Methods:
* A constructor
* The constructor for the class should accept at least two arguments: the title of the page, and an array of
* css filenames that it will use to create the appropriate code in the head section to link to those stylesheets
* The constructor should create the head section and footer section and give a default value for the body section
*
* addToBody
* a method called 'addToBody' that will add text to the body attribute of the webpage.  See the client to see how this method
* will be used - it'll give you a clue as to how to implement it.
*
* getPage
* a getPage method which has as a return value the various sections, head, body and footer, of the webpage concatenated together.
*
* Consider carefully the scope of all attributes and methods/functions.  Remember to make scope as restrictive
* as possible while still being consonant with the class working.
*
*/

?>

usewebpage.php

<?php

/**
 *
 * @version $Id$
 * @copyright 2008
 */
// require the class definition file
require_once( 'webpage.class.php' );

// create a new instance of the webpage class passing the title and an array of stylesheets to the constructor
$page = new Webpage ("test", array( "dream.css", "main.css" ) );

// add new text to the page body
$page->addToBody( "\n<p>This is my first page constructed with my webpage class</p>" );
$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>");
$page->addToBody( "<div id="header"><img src="images/needs2007_small.png" />");
$page->addToBody( "<h1>Header</h1>");
$page->addToBody( "</div>");

// echo the page contents back to the browser
echo $page->getPage();


?>

is the code on usewebpage.php theirs or yours?

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.

Class Webpage {

	private $head;
	private $addToBody;
	private $cssFiles;

	function __construct($head, $addToBody, $cssFiles) {
    // Store the doctype into the attribute of the class
        $this->head = $head;
        $this->addToBody = $addToBody;
		$this->styles = $cssFiles;
        //$this->styles = array() ??????;

        } 

	function addToBody($toAdd){
   		$this->addToBody .= $toAdd;
   		return;
		}
		
	function styles($styles){
		$this->styles .= $styles;
		//echo "<link href=\"css/dream.css\" rel=\"stylesheet\" type=\"text/css\" />";
		return;
		}

	function getPage(){
  		return $this->head . $this->addToBody . $this->styles;
		}

	}
<?php

/**
 *
 * @version $Id$
 * @copyright 2008
 */
// require the class definition file
require_once( 'webpage.class.php' );

// create a new instance of the webpage class passing the title and an array of stylesheets to the constructor
$page = new Webpage  ($head, $addToBody, $cssFiles);

// add new text to the page body
//$page->head("<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">);
//$page->head ("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
//$page->head ("<head>");
//$page->head ("<meta http-equiv=\"Content-Type\" content=\"text/html\; charset=\utf-8\" />");
//$page->head ("<title>Home</title>");
$page->styles("<link href=\"css/project.css\" rel=\"stylesheet\" type=\"text/css\" />");
$page->addToBody( "\n<p>This is my first page constructed with my webpage class</p>" );
$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>");
$page->addToBody( "<div id=\"open\"><img src=\"images/needs2007_small.png\" />");
$page->addToBody( "<h1>Header</h1>");
$page->addToBody( "</div>");

// echo the page contents back to the browser
echo $page->getPage();


?>

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.

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.

In my opinion is much better tu use something like this:

Template.class.php:

<?php

class Template
{
	private $tpl_data = array();
	private $file;
	
	function __construct($filename)
	{
		// Load file into $file var
		$this->file = implode('', @file($filename));
	}
	
	public function assign($vardata)
	{
		// Load var array into $tpl_data
		foreach($vars as $key => $value) {
			$this->tpl_data[$key] = $value;
		}
	}
	
	public function run()
	{
		// replace {$key} into $value
		foreach($this->tpl_data as $key => $value) {
			$this->file = str_replace('{' . $key . '}', $value, $this->file);
			echo $this->file;
		}
	}
}

?>

template.html

<html>
  <head>
    <title>{TITLE}</title>
  </head>
  <body>
    <h1>{WELCOME}</h1>
  </body>
</html>

show_page.php

<?php

$tpl = new Template("template.html");
$tpl->assign(array(
	'TITLE' => 'Just a test',
	'WELCOME' => 'Welcome on our pages'
));
$tpl->run();

?>

In my opinion is much better tu use something like this:

Template.class.php:

<?php

class Template
{
	private $tpl_data = array();
	private $file;
	
	function __construct($filename)
	{
		// Load file into $file var
		$this->file = implode('', @file($filename));
	}
	
	public function assign($vardata)
	{
		// Load var array into $tpl_data
		foreach($vars as $key => $value) {
			$this->tpl_data[$key] = $value;
		}
	}
	
	public function run()
	{
		// replace {$key} into $value
		foreach($this->tpl_data as $key => $value) {
			$this->file = str_replace('{' . $key . '}', $value, $this->file);
			echo $this->file;
		}
	}
}

?>

template.html

<html>
  <head>
    <title>{TITLE}</title>
  </head>
  <body>
    <h1>{WELCOME}</h1>
  </body>
</html>

show_page.php

<?php

$tpl = new Template("template.html");
$tpl->assign(array(
	'TITLE' => 'Just a test',
	'WELCOME' => 'Welcome on our pages'
));
$tpl->run();

?>

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

Ou. Yes of course should be $vardata. Thanks for correction. But I can't edit my post after 30 minutes :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.